hasOwnProperty: is used to determine whether an object has a property or object that you give a name to, and this method cannot check whether the object has that property in its prototype chain, and that property must be a member of the object itself.
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.
The instanceof action checks whether the object is named property. You can also check the prototype of an object to see if the property is part of the prototype chain.
Java code
hasOwnProperty:
var obj = {A:1,b:2}
Obj.hasownproperty (' a ')
isPrototypeOf:
function F () {}
var fn = new F ()
F.prototype.isprototypeof (FN)
The former is to determine whether a property exists in the object, and the latter is the object that determines whether the object is a prototype chain.
The isPrototypeOf function method in JavaScript is to return a Boolean value that indicates whether the object exists in the prototype chain of another object. How to use:
Object1.isprototypeof (OBJECT2)
Where Object1 is the required option, an instance of an object.
Object2 is the required option, and the other object is going to check its prototype chain.
If Object2 's prototype chain contains Object1, the isPrototypeOf function method in JavaScript returns TRUE.
A prototype chain can be used to share functionality between different instances of the same object type.
If Object2 is not an object or Object1 does not appear in the prototype chain in Object2,
The isPrototypeOf function method in JavaScript will return false.
The following example illustrates the use of the isPrototypeOf function method in JavaScript.
function Test () {
var re = new RegExp (); Initializes the variable.
Return (RegExp.prototype.isPrototypeOf (re)); Returns True.
}
Constructor: Object constructor.
Prototype: Accesses the prototype of the object constructor, only the function has this property.
isPrototypeOf: If object A exists in the prototype chain of object obj, a.isprototypeof (obj) returns True, and obj must inherit the attribute of a.
__PROTO__: The Access object's prototype chain in the current image position of the upper-level object, that is, the object's parent object, non-web or ECMAscript standard, is a browser to the prototype inheritance of a means of implementation, exist in Firefox and Chrome, IE does not exist this attribute.
In an object's inheritance relationship, the constructor constructor of the object obj is actually present in the prototype chain,
Namely Obj.constructor is actually Obj.__proto__.constructor, Obj.hasownproperty (' constructor '); to False
function Y () {this.y=99;}
var obj = new Y ();
Console.log (Obj.constructor); Y
Console.log (Obj.hasownproperty (' constructor ')); False
Console.log (Obj.__proto__.hasownproperty (' constructor ')); True
//=====
function X () {this.x=88;}
function Y () {this.y=99;}
Y.prototype = new X ();
var obj = new Y ();
Console.log (Obj.constructor); X
Console.log (Obj.hasownproperty (' constructor '));//false
Console.log (Obj.__proto__.hasownproperty (' constructor ')); False
Console.log (Obj.__proto__.__proto__.hasownproperty (' constructor ')); True
When accessing an object, there must be obj.x=value for Obj.__proto__.x=value.
The properties on the Obj.constructor.prototype will be saved in the obj.__proto__.
function Y () {this.y=99;}
Y.prototype = {A:11};
var obj = new Y ();
y.prototype.a=77;
Console.log ([obj.a,obj.__proto__.a,obj.y,obj.__proto__.y]);
/*77,77,99,OBJ.__PROTO__.Y is the Undefined,y property is generated directly by the object constructor, not from the prototype chain.
After the new operation constructs the Obj object, obj accesses the inherited property by __proto__, rather than through Obj.constructor.prototype access.
Therefore, if you modify Obj.constructor.prototype to point to another object, it does not affect obj inheriting the original property.
Y.prototype = {b:22};
Console.log (OBJ.B); Undefined
Console.log (OBJ.A); 77
In: If the object obj has property properties (both inherited and non-enumerated, unlike the in,for in loop that ignores non-enumerated properties), then ' property ' in obj returns True, this operation does not exist in the initial version of Java Script
propertyIsEnumerable: If the attribute property on the object obj can be enumerated (can be traversed by a in loop), then obj.propertyisenumerable (' Properties ') returns True, It is important to note that propertyIsEnumerable evaluates inherited attributes as false, which is generally considered a design error in the ECMA Script specification.
hasOwnProperty: If the attribute property on the object obj is not inherited, then Obj.hasownproperty (' Properties ') returns True.
Delete: Delete The property on the object itself, cannot delete the inherited property, in the old browser, cannot delete window.x by the use of the Var declaration of the global variable x, but in the latest browser is ready, including IE 9.
var f = function () {};
F.prototype = {x:99};
var o = new F;
Console.log (O.hasownproperty (' x ')); False
Console.log (o.x); 99
Delete o.x;
Console.log (o.x); 99
var x = 1;
Window.hasownproperty (' x '); True
Delete window.x;
Console.log (x); ERROR,X is not defined
instanceof: If the Obj object is an instance of a constructor fun, then obj instanceof fun returns True, and it is worth noting that instanceof does not check the fun function, but rather checks Fun.prototype, based on the "prototype chain" , so even if obj instanceof fun returns true,obj It may not have properties defined in the fun constructor, because fun is not necessarily the constructor of obj.
function Bar () {}
function A () {}
Bar.prototype = new A ();
var bar = new Bar ();
Console.log (bar instanceof Bar); True
Bar.prototype = new A ();
Console.log (bar instanceof Bar); False
The/*instanceof detects the prototype of the function, and the latter is not equal to the previous new A, (the objects are not equal) */
For instanceof, try the following code:
function P () {this.p=11;};
var pro = new P ();
function X () {this.x=88;}
function Y () {this.y=99;}
Y.prototype =pro;
var obj = new Y ();
1. Where is the object builder?
Console.log (Obj.hasownproperty (' constructor ')); False
Console.log (Obj.constructor); P
Console.log (obj.__proto__.constructor);//p
Console.log (Obj.__proto__.constructor = = = Y.prototype.constructor); True
This shows that when you execute new, obj.constructor that is Obj.__proto__.constructor is actually y.prototype.constructor instead of Y.
2. Object Builder Repair
But, a little bit of a problem, consider the Y property:
Console.log (OBJ.Y); 99
Console.log (OBJ.__PROTO__.Y); Undefined
Since the Y attribute is not from the prototype chain, it naturally comes from the object constructor, but the P function does not define the Y property.
From the "Inheritance Chain" formed by "class-Type Inheritance", P is just the source of "inheritance chain", that is, the top-level "base class", and the construction of obj object instance is derived from the "subclass" Y function.
This is a little bit of a rift between "simulating class inheritance" new and "prototype inheriting" prototype in the JS object inheritance system,
Many people are obsessed with repairing this crack, so there is the practice of fixing the constructor.
The default state is to declare a function fun, there are fun.prototype.constructor===fun, so:
Obj.constructor = Y; Fix the constructor and assign a constructor property to overwrite the constructor on the inheritance chain
Console.log (Obj.hasownproperty (' constructor ')); True
Console.log (Obj.constructor); Y
Console.log (Obj.__proto__.constructor); P
3, obj instancof fun is true does not mean that fun must be the constructor of the Obj object, fun may not exist in the inheritance chain of obj:
X.prototype = Pro;
Console.log (obj instanceof X); True
Console.log (obj.x);//undefined, X is not a constructor for obj
Console.log (obj instanceof Y); True
Console.log (OBJ.Y);//99
In the above code, if the P function is changed to
function K () {this.k=66;}
function P () {this.p=11;};
P.prototype = new K ();
Then, before repairing the constructor, the constructor on the inheritance chain will be k instead of p, and there will be:
Console.log (obj instanceof X); True
Console.log (obj.x);//undefined
Console.log (obj instanceof Y); True
Console.log (OBJ.Y);//99
Console.log (obj instanceof K); True
Console.log (OBJ.K); 66
4, Object.create ()
The new version of ECMAScript extends some methods for object objects,
Object.create (PRO) can create an object based on pro as a prototype, with an effect equivalent to
var f = function () {};
F.prototype = Pro;
var obj = new F ();
Try
function P () {this.p=11;}
function K () {this.k=22;}
function F () {this.f=33;}
var pro = new K ();
P.prototype = Pro;
var o= new P ();
var obj = object.create (o);
Console.log (O,obj); It's all {p:11,k:22}
Console.log (Obj.constructor); K
Console.log (Obj.__proto__.constructor); K
Console.log (obj instanceof P); True
Console.log (obj instanceof K); True
Console.log (obj instanceof F); False
F.prototype = Pro;
Console.log (obj instanceof F); True
5. Object and Function:
Console.log (Function instanceof Object); True
Console.log (Object instanceof Function); True
Is there a Function or an Object first? The following phenomenon may explain that object is the "top"
Console.log (Object.__proto__.__proto__===function.prototype); False
Console.log (function.__proto__.__proto__ = = = Object.prototype); True
Console.log (object.prototype.__proto__); Null, object prototype is already the Nu WA class.
JavaScript in isPrototypeOf, hasOwnProperty, constructor, prototype and other usages