MENU
Object â Prototype Properties and Methods
Properties
Object.prototype.constructor
Specifies the function that creates an object's prototype. All objects will have a constructor property. Objects created without the explicit use of a constructor function (i.e. the object and array literals) will have a constructor property that points to the Fundamental Object constructor type for that object.
var o = {};
console.log(o.constructor === Object, o.constructor.name);
var o = new Object;
console.log(o.constructor === Object, o.constructor.name);
var a = [];
console.log(a.constructor === Array, o.constructor.name);
var a = new Array;
console.log(a.constructor === Array, o.constructor.name);
var n = new Number(3);
console.log(n.constructor === Number, o.constructor.name);
function C() {
this.x = 100;
}
console.log(C);
console.log(C.constructor);
console.log(C.constructor.name);The following shows how to test for {}.
var o = {};
console.log(Object.keys(o).length === 0 && o.constructor === Object);Object.prototype.__proto__
Points to the object which was used as the prototype when the object was instantiated, exposing [[Prototype]] (referto 8.2.8 for more). Its use has been controversial but has been standardized in ECMAScript-2015. It is recommended to use Object.getPrototypeOf() instead.
var C = function () {};
var p = {
a: function () {
console.log('a');
}
};
C.prototype.__proto__ = p;
var o = new C();
o.a();
console.log(C.prototype === o.__proto__);
var o = new C();
o.__proto__ = p;
o.a();
console.log(C.prototype === o.__proto__);function test() {}
test.prototype.myname = function () {
console.log('Hello');
};
var a = new test;
console.log(a.__proto__ === test.prototype);
a.myname();
var obj = { __proto__: test.prototype };
obj.myname();Value s
Object.prototype.valueOf()
Returns the primitive value of the specified object. This function is called automatically when the object is treated as a primitive which is not a string.
function N() {
this.x = 1;
}
N.prototype.valueOf = function () {
this.x++;
return this.x;
};
var n = new N;
console.log(n);
console.log(n + 1);
console.log(n);
console.log(n + n); // ⦠3 + 3 + 1Object.prototype.toString()
Returns a string representation of the object. This function is called automatically when the object is treated as a string.
function C() {}
var o = new C;
console.log(C + "");
console.log(o + "");
C.prototype.toString = function a() {
return "Haha!";
};
console.log(C + "");
console.log(o + "");Object.prototype.toLocaleString()
Calls toString().This method is meant to be overridden by derived objects for locale-specific purposes.
Check s
Object.prototype.hasOwnProperty()
Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
o = {};
o.a = 10;
console.log('a' in o, '__proto__' in o);
console.log(o.hasOwnProperty('a'), o.hasOwnProperty('__proto__'));function C() {
this.a = 1;
}
C.prototype.b = 2;
var o = new C;
console.log(o.b);
for (let p in o) {
console.log(p);
if (o.hasOwnProperty(p))
console.log("ownProperty: " + p);
}Object.prototype.isPrototypeOf( X )
Returns a boolean indicating whether the object this method is called upon is in the prototype chain of X. Unlike 'intanceof X' which compares against X, .isPrototypeOf(X) compares against X.prototype.
function A() {}
function B() {}
function C() {}
B.prototype = Object.create(A.prototype);
C.prototype = Object.create(B.prototype);
var o = new C();
console.log(o instanceof C, C.prototype.isPrototypeOf(o));
console.log(o instanceof B, B.prototype.isPrototypeOf(o));
console.log(o instanceof A, A.prototype.isPrototypeOf(o));
console.log(o instanceof Object, Object.prototype.isPrototypeOf(o));Object.prototype.propertyIsEnumerable()
Returns a boolean indicating if the internal ECMAScript [[Enumerable]] attribute is set, ie. if a property is enumerable by a for...in loop, etc., except properties inherited through the prototype chain.
var a = [10, 20];
a.v = 30;
console.log(a.propertyIsEnumerable(0));
console.log(a.propertyIsEnumerable('v'));
console.log(a.propertyIsEnumerable('constructor'));
console.log(a.propertyIsEnumerable('length'));
console.log(this.propertyIsEnumerable('Math'));
console.log(Math.propertyIsEnumerable('random'));function A() {
this.property = 'is not enumerable';
}
A.prototype.m1 = function () {};
function B() {
this.m2 = function () {
return 'is enumerable';
};
}
B.prototype = new A;
B.prototype.constructor = B;
var o = new B;
o.x = 99;
console.log(o.propertyIsEnumerable('x'));
console.log(o.propertyIsEnumerable('m2'));
console.log(o.propertyIsEnumerable('property'));
console.log(o.propertyIsEnumerable('prototype'));
console.log(o.propertyIsEnumerable('constructor'));
console.log(o.propertyIsEnumerable('m1'));