Review: . 1 , function Student (name, Age) { the this .name = name; the this .age = Age; } Student.prototype = {// perform one of the sayHello: function () { the console.log ( "NiHao" ) ; } }; var P = new new Student ( 'zhansa',. 19 ); code analysis: 1 , the first pre-analytical process, declare constructors Student, while constructing the prototype object student.Prototype
2 , first sentence of the first line perform . Re-cover the prototype object. The original object is no longer the default point
3 , to create objects, new objects, then this points to the current object's constructor
4 , inside the constructor, using the object dynamic characteristics to the subject attribute just created it provides name, age A) constructor incoming call when the argument, 'zhansa', 19 B) is therefore the object properties. . . After c) object initialization is completed, the return address of the object to the variable P
. 5 , the calling method. p call sayHello method a) First check object is not pointed to by p sayHello method b) then in the prototype objects, Student.prototype in c) there sayHello new prototype object, so to call it d) in which method Print
Object. The Create method
Is es5 new method introduced.
Syntax :
Object. The Create Prototype (Object) Returns a new object and returns a new object that is represented in the object parameter
Function : implementation inheritance, inherited from prototype to create a parameter object
E.g:
var O = { the sayHello: function () {} }; var O1 = the Object.create (O); // create an object o1, i.e., the prototype of the new object is __proto__ O o1.sayHello (); // JS object is a collection of key-value pairs [key: value} var ARR = the Object.create ([]); // . 1, the stored data arr.push (. 1 ); arr.push ( 2 ); arr.push ( 2,3, 4,5,6,7 ); // 2, traversal for ( var I = 0; I <arr.length; I ++ ) { the console.log ( '[' + I + '] =' + ARR [I]); }
Exercise: If your browser does not support . Object the Create how to do
var O = { the sayHello: function () {} }; var O1 = the Object.create (O); // create an object o1, i.e., the prototype of the new object is __proto__ O o1.sayHello (); // JS object is a collection of key-value pairs [key: value} var ARR = the Object.create ([]); // . 1, the stored data arr.push (. 1 ); arr.push ( 2 ); arr.push ( 2,3, 4,5,6,7 ); // 2, traversal for ( var I = 0; I <arr.length; I ++ ) { the console.log ( '[' + I + '] =' + ARR [I]); } // Do not modify the built-in objects, as follows: //IF (! the Object.create) { // the Object.create = function () {} // } // This function prototypes to achieve the inheritance, the object should be returned Inherited from obj function the inherit (the Obj) { IF (the Object.create) { return the Object.create (the Obj); } the else { function F. () {} F.prototype = the Obj; return new new F. (); } }
was arr inherit = ([]);
Object's prototype chain
1, every object has a prototype
2, the prototype also Object
problem
1 , the prototype chain in the end when the head 2 , a default prototype chain What is the structure of 3, the prototype chain structure known grammar correction
Prototype chain
Those who use the constructor, and there is no way to use the modified assignment prototype, created out of objects to keep the default prototype chain.
The default prototype chain structure is like?
The default is the current prototype chain structure of the object -> . Constructor Prototype-> . Object Prototype-> null
What is the prototypal inheritance
The so-called prototype style is the use of modified prototype inheritance chain structure (add, delete, modify) the afterlife instance of an object can be used all the members of the whole chain
Here's the rule.
In js , all object literal after parsing is a particular object constructor method call may be understood as corresponding to the
1, for example, the code is written in '{}', the equivalent of ' new new Object ()' 2, for example, the code is written in '[]', the equivalent of ' new new Array ()' . 3, for example, the code is written in ' /. / ', Is equivalent to' new new the Regexp () '
Note: During the execution of the underlying theory, if there is call the constructor, not necessarily with the version of the browser.
In js use Function can be instantiated function objects. That is a function object
1 , the object is a function, you can use the dynamic characteristics of the object 2 , the function is a function that can create additional objects 3, only the function can be the result variable scope defined
To solve the problem
1, Function How to Use
2, Function relationship with function
3, the prototype of the function of chain structure
Function is a Function instance
Syntax: new new Function ( arg0 , arg1 , arg2. ..)
. 1, Function of all parameters is a string
2, the role of the constructor parameter is wired up function
a) If only one parameter, the function body represents
b) If there are a plurality of parameters, then the last parameter indicates the function body, another parameter indicating
c) If there is no argument, it means creating an empty function
Arguments
Arguments is an array of objects representing all of the incoming set of parameters during the function call.
During the function call is not a predetermined number and type of parameters, and therefore function call flexibility.
For ease of use, a process in each function call, the body of the function code and a default object arguments The , which stores all the parameters actually passed.
js the function does not specify how to pass parameters to be
1 , defined functions when not write parameters can be passed as parameters when calling 2 , the definition of the parameters of time to write, call when you can not pass parameters 3, wrote a defined time parameters when the call can pass as many parameters
In the design of the code, with the function of any parameters required when generally not any parameters , all parameters using arguments to obtain
Function foo (/*。。。*/){}
function foo () { // all parameters in the arguments, which was as an array // problem var args = arguments; var max = args [0 ]; for ( var I =. 1; I <args.length; ++ I ) { IF (max < args [I]) { max = args [I]; } } return max; } the console.log (foo ( 1,2,3,4,34,2,34234));
Why use Function ?
Function Prototype chain structure:
任意的一个函数,都相当于Function的实例。类似于{}与new object()的关系
Function foo(){}
相当于告诉解释器,又一个对象foo,它是一个函数
相当于new Function(),因此:
1、函数应该有什么属性?‘__proto__’; 2、函数的构造函数Function() 3、函数继承自Function.Prototype 4、Function.prototype继承自object.Prototype
小结:
Object构造函数是Function的一个实例
Object作为对象继承自Function.prototype.又F.prototype继承自Object.prototype
Function是自己的构造函数(*****)
绘制Function的构造原型实例三角形结构
1、在js中任何对象的老祖宗就是Object.prototype
2、在js中任何函数的老祖宗就是Function.Prototype
小结:
每个函数都有prototype属性指向原型自定义的没有; 每个对象都有—proto-属性指向原型; __proto__属性是为了能够直接使用原型的方法。
小结:
Object.create最简单原型继承
对象原型链