First, we need to know that JavaScript differs from traditional object-oriented programming (OOP) in that it has no traditional class, and that everything in that language is object-based and relies on a set of prototype (prototype) systems. JavaScript implements inheritance between objects and objects through prototype delegates, rather than class-based inheritance in traditional object-oriented languages.
Variable type of dynamic type language when the program runs, the variable is assigned to a certain type, and JavaScript is a typical dynamic type language.
First, prototype mode
The prototype pattern is a pattern for creating objects that can be cloned to create an object, and the newest ECMASCRIPT5 provides the Object.create method to clone objects:
<script>varFruit={price:15,name: "Apple"}vardemo=object.create (fruit); alert (demo.name);//Apple
//or:functionSch () { This. Name= "Hube"; This. age=100;
}vartt=NewSch ();vart=object.create (TT); alert (t.age);//100
//in browsers that do not support this method, you can use the following Polyfill code:object.create=function(obj) {varf=function(){};//an implicit constructor is definedF.prototype=obj; return NewF ();//or through new .}</script>
Object.create()
Method uses the specified prototype object and its properties to create a new object. In fact, JavaScript has a root object, which is the object.prototype object, Object.prototype is an empty object, and every object we create is cloned from Object.prototype:
var t={}; // empty objects created in literal form are equivalent to: Var t= object.create (object.prototype); var s=New Object (); alert (object.getprototypeof (t)===object.prototype); // truealert (object.getprototypeof (s) ===object.prototype); // true
Two "empty" objects were created above, and the object.getprototypeof method of ECMAScript5 was used to find the prototype of two objects. The "empty" quotes here are not really empty objects, it also inherits some of the properties and methods of object. Create an empty object usingObject.create()即可:
var o = object.create (null);//Create an empty object with a null prototype
This mode is not limited to this, it is more of a convenient way to create a type of object.
Prototype Inheritance:
In fact, the prototype inheritance between "Class" and "class" is implemented by dynamically assigning to other objects the prototype of the object constructor:
var a=function() {}; A.prototype.sayname=function() {alert ("HI");}
var b=function() {}; B.prototype=new A (); // This is the core code: it overrides the prototype of B, and it has no essential difference compared to the direct assignment of B.prototype to a literal object, // is to point the prototype of the object constructor to another object, and inheritance always occurs between the object and the object. var b=New B (); B.sayname (); // Hi
Second, the single case mode
Three, the Strategy model
Iv. Proxy Mode
Cond
JS design mode