Introduction to some hidden functions of object objects

Source: Internet
Author: User
Tags constructor copy tostring hasownproperty
Object| Objects | functions

Properties: Object.constructor

This property is defined in the prototype of the class, which can be invoked by an object instance after the object instance is created and points to the constructor of the current class. This allows you to determine which class the object directly belongs to (unlike instanceof, instanceof is not limited to the class that the object directly belongs to, even if the parent class returns True).

[Example]trace (Object.prototype.constructor = = Object); Output true
var a = new Object ();
Trace (A.constructor = = Object); Output true
var B = new Array ();
Trace (B.constructor = = Array); Output true
Trace (B.constructor = = Object); Output false
Trace (b instanceof Object); Output true

Code Copy Box
Trace (Object.prototype.constructor = = Object); Output Truevar a = new Object (); trace (A.constructor = = object); Output Truevar B = new Array (); trace (b.constructor = = array); Output Truetrace (B.constructor = = Object); Output Falsetrace (b instanceof Object); Output true
[Ctrl + a All select and copy]

Properties: object.__constructor__

This property function is similar to Object.constructor, except that it is not defined in the prototype of the class, but rather when the object instance is created and appended to the object instance. Also, this property is implicitly invoked when the Super keyword is used as the parent class constructor to point to the constructor of the parent class, that is, super (...). Equivalent to This.__constructor__.call (this, ...).

[Example]trace (object.prototype.__constructor__ = = Object); Output false
var a = new Object ();
Trace (a.__constructor__ = = Object); Output true
var B = new Array ();
Trace (b.__constructor__ = = Array); Output true
Trace (b.__constructor__ = = Object); Output false

Code Copy Box
Trace (object.prototype.__constructor__ = = Object); Output Falsevar a = new Object (); trace (a.__constructor__ = = object); Output Truevar B = new Array (); trace (b.__constructor__ = = array); Output Truetrace (b.__constructor__ = = Object); Output false
[Ctrl + a All select and copy]

Method: Object.isprototypeof (Classfunc)

This method is used to determine whether the current object is in the __proto__ chain of object obj. This method can be used to determine whether a class is a parent class or subclass of another class.

[Example]trace Object.prototype.isPrototypeOf (New Object ()); Output true
Trace (Object.prototype.isPrototypeOf) (New Array ()); Output true
Trace (Array.prototype.isPrototypeOf (New Object ()); Output false
Trace (Object.prototype.isPrototypeOf (array.prototype)); Determines whether object is a parent of array, and outputs true

Code Copy Box
Trace (Object.prototype.isPrototypeOf (New Object ()); Output Truetrace (new Array ()) (OBJECT.PROTOTYPE.ISPROTOTYPEOF); Output Truetrace (Array.prototype.isPrototypeOf (New Object ()); Output Falsetrace (Object.prototype.isPrototypeOf (Array.prototype)); Determines whether object is a parent of array, and outputs true
[Ctrl + a All select and copy]

Method: Object.ispropertyenumerable (propname)

This method is used to determine whether a member named PropName exists in the current object and can be enumerated (using for ...). In, in other words, is visible (use the Assetpropflags global function to set whether the object property is visible).

[Example]var a = {x:1, y:2};
Assetpropflags (A, ["Y"], 1); Set Y to Invisible
Trace (A.Y); can still output 2
for (var i in a) trace (i); Output x only
Trace (A.ispropertyenumerable ("X")); Output true
Trace (a.ispropertyenumerable ("Y")); Output false

Code Copy Box
var a = {x:1, y:2}; Assetpropflags (A, ["Y"], 1); Set Y to invisible Trace (A.Y); can still output 2for (var i in a) trace (i); Output only Xtrace (A.ispropertyenumerable ("X")); Output Truetrace (a.ispropertyenumerable ("Y")); Output false
[Ctrl + a All select and copy]

Method: Object.hasownproperty (propname)

This method is used to determine whether a member named PropName is a member of the current object, rather than being referenced by the __proto__ chain from the prototype of the class.

[Sample]function Test () {}
test.prototype.x = 1;
var a = new test ();
A.Y = 2;
Trace (a.x); Output 1
Trace (A.hasownproperty ("X")); Output false
Trace (A.Y); Output 2
Trace (A.hasownproperty ("Y")); Output true

Code Copy Box
function test () {}test.prototype.x = 1;var a = new test (); a.y = 2;trace (a.x); Output 1trace (A.hasownproperty ("X")); Output Falsetrace (A.Y); Output 2trace (A.hasownproperty ("Y")); Output true
[Ctrl + a All select and copy]

Method: Object.ToString ()

This method defines the string result that an object produces when converting to a string type, which is generally defined in the prototype of the class.

Example

function point (x, y) {
this.x = x;
This.y = y;
}
point.prototype.toString = function () {
Return "[x:" + this.x + ", y:" + this.y +] ";
};
var pos = new Point (10, 20);
Trace ("Position is" + pos); Output position is [X:10, y:20]

Code Copy Box
function point (x, y) {this.x = x; This.y = y;} point.prototype.toString = function () {return "[x: + this.x + +", y: "+ this.y +]";}; var pos = new Point (a), trace ("Position is" + pos); Output position is [X:10, y:20]
[Ctrl + a All select and copy]



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.