Common properties and methods in object-oriented

Source: Internet
Author: User

The origin of the picture looks below:

1, hasOwnProperty: See is not the object itself below the properties

var arr =ten;    //All array objects have num2, not just the properties of arr itself  // Alert (  arr.hasownproperty (' num ')  );   // true Alert (  arr.hasownproperty ('num2')  );  // false

2, constructor: View the object's constructor, each prototype will automatically generate it, avoid modifying it.

function Aaa () {} var New Aaa (); alert (a1.constructor);   // Aaa

This is our custom object and then bounced out of AAA. Below we can try the array.

var arr = [];alert (Arr.constructor);   // Array

It is therefore possible to use a way to determine whether an array is. Arr.constructor = = Array is true, or other types are also available.

While constructor is available for every function, the constructor generates only this constructor. We can also use the constructor. Prototype.construstor to modify it, of course, it is best to modify less.

function Aaa () {} // Aaa.prototype.constructor = Aaa;    // every function will have, it's automatically generated. Aaa.prototype.constructor = Array; var New Aaa (); alert (a1.constructor)    // Array

It is conceivable that the constructor is under the prototype of the constructor, but the hasOwnProperty is under the object prototype

var New  = = Aaa.prototype.constructor);   // true; alert (A1.hasownproperty = = Object.prototype.hasOwnProperty);  // true

3. Instance: Is there any relationship between the object and the constructor on the prototype chain?

 function Aaa () {}  var  a1 = new   Aaa ();  //  alert (A1 instanceof Aaa);  // true   alert (A1 instanceof Object);  // true   alert (A1 instanceof Array);  // false   arr = [];alert (arr instanceof Array);  // true  

So there's another way to determine if it's an array: arr instanceof array

4. toString

Let's think about where it is.

// toString (): The system objects are all self-array.prototype (for example, below), and their own objects are found under object through the prototype chain (for example, Object.prototype.toString)
var arr = [];
Alert (arr.tostring = = Object.prototype.toString); False
Alert (arr.tostring = = Array.prototype.toString); True

function Aaa () {
}
var a1 = new Aaa ();
Alert (a1.tostring = = Object.prototype.toString); True
Alert (a1.tostring = = Array.prototype.toString); False

Now that we know where it is, it is natural for us to transform it. ToString converts the object to a string, but we want to change it in a different way.

/* var arr = [n/a]; Array.prototype.toString = function () {    return This.join (' + ');}; Alert (arr.tostring ());  ' 1+2+3 '* *

You can also use the ToString conversion, such as ToString (16), to convert to 16 binary.

The next point is to determine whether the best way to array is: Object.prototype.toString.call (arr) = = ' [Object array] '

/**//' [Object Array] '
var arr = {}; alert (Object.prototype.toString.call (arr));    // Object Object var New Date; alert (Object.prototype.toString.call (arr));    // Object Date var New REGEXP (); Alert (Object.prototype.toString.call (arr));    // Object RegExp

Common properties and methods in object-oriented

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.