Object – Prototype Properties and Methods

Properties Properties 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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

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);// true "Object"
   (for each line)function C(){this.x=100;}console.log(C);        // function C(){this.x=100;}console.log(C.constructor);                               // function Function() { [native code] }console.log(C.constructor.name);  // Function

</script></body><html>

The following shows how to test for {}.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var o = {};console.log(Object.keys(o).length === 0 &&
                  o.constructor === Object);  // true

</script></body><html>

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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var C = function () {};var p = {
    a: function () {
        console.log('a');
    }};C.prototype.__proto__ = p;var o = new C();o.a(); //aconsole.log(C.prototype === o.__proto__);  //truevar o = new C();o.__proto__ = p;o.a(); //
  aconsole.log(C.prototype === o.__proto__);  //false

</script></body><html>

<!DOCTYPE html><html><body><script>

function test() {}test.prototype.myname = function () {
    console.log('Hello');}var a = new test;console.log(a.__proto__ === test.prototype);//truea.myname(); // Hellovar obj = {__proto__: test.prototype};obj.myname(); // Hello

</script></body><html>

Value Value Value s s 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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

function N() {this.x=1;}N.prototype.valueOf = function(){this.x++;return this.x;}var n = new N;console.log(n);     // N {x: 1}console.log(n+1); // 3console.log(n);     // N {x: 2}console.log(n+n); // 73 + 3 + 1

</script></body><html>

Object.prototype.toString()

Returns a string representation of the object. This function is called automatically when the object is treated as a string.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

function C() {}var o = new C;console.log(C+"");  // function C() {}console.log(o+"");  // [object Object]C.prototype.toString = function a(){return "Haha!";}console.log(C+"");  // function C() {}console.log(o+"");  // Haha!

</script></body><html>

Object.prototype.toLocaleString()

Calls toString().This method is meant to be overridden by derived objects for locale-specific purposes.

Check Check Check s s 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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

o = {}; o.a = 10;console.log('a' in o, '__proto__' in o);            // true trueconsole.log(o.hasOwnProperty('a'),
                 o.hasOwnProperty('__proto__')); // true false

</script></body><html>

<!DOCTYPE html><html><body><script>

function C(){this.a = 1;}C.prototype.b = 2;var o = new C;console.log(o.b);  // 2for (let p in o) {
    console.log(p);if (o.hasOwnProperty(p))
     console.log("ownProperty: "+p);}   // a
   ownProperty: a
     b

</script></body><html>

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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

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));         // true trueconsole.log(o instanceof B
       ,
                  B.prototype.isPrototypeOf(o));         // true trueconsole.log(o instanceof A
       ,
                  A.prototype.isPrototypeOf(o));         // true trueconsole.log(o instanceof Object,
                  Object.prototype.isPrototypeOf(o)); // true true

</script></body><html>

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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var a = [10,20];
  a.v=30;console.log(a.propertyIsEnumerable(0));                 // trueconsole.log(a.propertyIsEnumerable('v'));                // trueconsole.log(a.propertyIsEnumerable('constructor')); // falseconsole.log(a.propertyIsEnumerable('length'));        // falseconsole.log(this.propertyIsEnumerable('Math'));      // falseconsole.log(Math.propertyIsEnumerable('random'));// false

</script></body><html>

<!DOCTYPE html><html><body><script>

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'));               // trueconsole.log(o.propertyIsEnumerable('m2'));            // trueconsole.log(o.propertyIsEnumerable('property'));     // falseconsole.log(o.propertyIsEnumerable('prototype'));   // falseconsole.log(o.propertyIsEnumerable('constructor')); // falseconsole.log(o.propertyIsEnumerable('m1'));             // false

</script></body><html>