transferred from: http://www.cnblogs.com/ArthurXml/p/6555509.html1, hasOwnProperty and in
First to understand the hasOwnProperty method. This method is used to examine the object's non-prototype chain properties, in other words, to check the user-defined attributes in the object, and these properties are not defined on prototype. This is understood by the following code:
varMyfunc=function(){ This. foo= ' Value '; };myfunc.prototype.ok= ' OK '; Thefunc=NewMyFunc (); Console.log (Thefunc.hasownproperty (' foo '),//trueThefunc.hasownproperty (' OK '),//falseMyfunc.hasownproperty (' foo '),//falseMyFunc.prototype.hasOwnProperty (' OK '),//true Here's a little special' OK 'inchThefunc,//true' Foo 'inchThefunc,//true' OK 'inchMyfunc.prototype,//true' OK 'inchMyFunc//false);
In the above code, Foo is a user-defined non-prototype property, and OK is a prototype attribute defined on prototype, so the value of Thefunc.hasownproperty (' foo ') is True,thefunc.hasownproperty ( The value of ' OK ' is false.
There is also an in operator in the JavaScript language that examines the properties of an object, including properties from the prototype chain.
2, constructor
Each JavaScript function (except for the function returned by Function.bind () in ECMAScript 5) automatically has a prototype property. The value of this property is an object that contains the only non-enumerable property, constructor. The constructor instance has a constructor property that points to its constructor. The value of the constructor property is a function object:
var f=function() {}; // This is a function object var P=f.prototype; // This is an F-associated prototype object var C=p.constructor; // this is the function associated with the prototype. C===f; // true, for any function f.prototype.constructor===f // excerpt from the authoritative guide to JavaScript
Insert the code in the first section here and expand some of the relationships that you want to be able to explain the constructors and prototypes:
var myfunc=function() { this. foo= ' value'; }; MyFunc.prototype.ok= ' OK '; thefunc=new myFunc (); Console.log ( thefunc.constructor //true myfunc.constructor===function,//true typeof Thefunc,//object myfunc.prototype//object () {ok= ' OK '});
3, instanceof and isprototypeof
Instanceof is an operator in the JavaScript language. The left operand is the object whose class is to be detected, and the right operand is the constructor that defines the class. Therefore, when you use the instanceof operator for an object that is not defined using the literal form of the constructor, its constructor cannot be found.
isPrototypeOf is used to determine whether the object whose prototype chain is to be checked exists in the specified object instance, or True, otherwise returns false.
varMyfunc=function(){ This. foo= ' Value '; };myfunc.prototype.ok= ' OK '; Thefunc=NewMyFunc (); Console.log (ThefuncinstanceofMyFunc,//trueMyFuncinstanceofFunction,//trueMyFunc.prototype.isPrototypeOf (Thefunc),//trueFunction.prototype.isPrototypeOf (MyFunc),//trueMyfunc.prototype,//Object () {ok= ' OK '} typeofThefunc,//ObjectThefunc.prototype//Undefined, we don't understand.);
Careful taste will find the difference between the two main:
A.isprototypeof (b) Determines whether a object exists in the prototype chain of the B object.
A instanceof B determines whether B.prototype exists in a prototype chain with a
So there is the following conclusion:
If a.isprototypeof (b) returns true Then B instanceof A must return True
4, typeof
The typeof is a unary operator placed before the operand and can be of any type. The return value is a string representing the type of operand. typeof is the best way to identify the original type.
Here are the test cases for the above operations:
varMyNumber =NewNumber (' 23 ');varMynumberl = 23;//literal shorthandvarMyString =NewString (' Male ');varMystringl = ' Male ';//literal shorthandvarMyboolean =NewBoolean (' True ');varMYBOOLEANL =true;//literal shorthandvarMyObject =NewObject ();varMyobjectl = {};//literal shorthandvarMyArray =NewArray ();varMyarrayl = [];//literal shorthandvarMyFunction =NewFunction ();varMyfunctionl =function() {};//literal shorthandvarMyDate =NewDate ();varMyregexp =NewREGEXP ('/./');varMYREGEXPL =/./;//literal shorthandvarMyerror =NewError (); Console.log (//All of these return trueMynumber.constructor = = =Number , Mynumberl.constructor===Number , Mystring.constructor===String, Mystringl.constructor===String, Myboolean.constructor===Boolean, Mybooleanl.constructor===Boolean, Myobject.constructor===Object, Myobjectl.constructor===Object, Myarray.constructor===Array, Myarrayl.constructor===Array, Myfunction.constructor===Function, Myfunctionl.constructor===Function, Mydate.constructor===Date, Myregexp.constructor===RegExp, Myregexpl.constructor===RegExp, Myerror.constructor===Error); Console.log (//others return TrueMyNumberinstanceofNumber , MynumberlinstanceofNumber,//falseMyStringinstanceofString, MystringlinstanceofString,//falseMybooleaninstanceofBoolean, mybooleanlinstanceofBoolean,//falseMyObjectinstanceofObject, MyobjectlinstanceofObject, MyArrayinstanceofArray, MyarraylinstanceofArray, MyFunctioninstanceofFunction, MyfunctionlinstanceofFunction, MyDateinstanceofDate, MyregexpinstanceofRegExp, MyregexplinstanceofRegExp, MyerrorinstanceofError); Console.log (//others return TrueNumber.prototype.isPrototypeOf (MyNumber), Number.prototype.isPrototypeOf (Mynumberl),//falseString.prototype.isPrototypeOf (myString), String.prototype.isPrototypeOf (MYSTRINGL),//falseBoolean.prototype.isPrototypeOf (Myboolean), Boolean.prototype.isPrototypeOf (mybooleanl),//falseObject.prototype.isPrototypeOf (MyObject), Object.prototype.isPrototypeOf (MYOBJECTL), Array.prototype.isProto TypeOf (MyArray), Array.prototype.isPrototypeOf (Myarrayl), Function.prototype.isPrototypeOf (myFunction), Function. Prototype.isprototypeof (myfunctionl), Date.prototype.isPrototypeOf (mydate), RegExp.prototype.isPrototypeOf ( MYREGEXP), RegExp.prototype.isPrototypeOf (MYREGEXPL), Error.prototype.isPrototypeOf (Myerror)); Console.log (typeofMyNumber,//Object typeofMynumberl,// Number typeofMyString,//Object typeofMystringl,//string typeofMyboolean,//Object typeofMYBOOLEANL,//Boolean typeofMyObject,//Object typeofMyobjectl,//Object typeofMyArray,//Object typeofMyarrayl,//Object typeofMyFunction,//function typeofMyfunctionl,//function typeofMyDate,////object typeofMyregexp,//Object typeofMYREGEXPL,//Object typeofMyerror//Object);
Constructor, instanceof, isPrototypeOf, typeof, and hasOwnProperty in JavaScript