Straighten out JavaScript (14)-constructor and instanceof

Source: Internet
Author: User
Let's repeat the previous example: It's a function. How can it become an object?
 
Function myobj (a, B) {This. X = A; this. y = B;} var OBJ = new myobj (11, 22); alert (obj. x); // 11 alert (obj. y); // 22 alert (typeof OBJ); // object // an object must be created through a constructor. It is not difficult to become an object with a constructor; // use the new keyword to call a function. Javascript creates an object and treats the function as a constructor of the object. // This is like the constructor of the string class. The constructor of the array class is array ()...

  

The name of the constructor of each class must be different. However, you can use constructor to refer to them.

 
VaR STR = new string (); alert (Str. constructor);/* will output the following: function string () {[native code]}, but it does not show us the specific implementation.Code, Only the custom one will see */var arr = new array (); alert (ARR. constructor);/* will output the following: Function Array () {[native code]} */function myobj (a, B) {This. X = A; this. y = B;} var OBJ = new myobj (11, 22); alert (obj. constructor);/* will output the following: function myobj (a, B) {This. X = A; this. y = B ;}*/

  

Determine the class to which an object belongs (method 1)

 
// The preceding method can be used to determine the VaR STR, arr, s; STR = new string (); alert (Str. constructor = string); // truearr = new array (); alert (ARR. constructor = array); // true/* but this may cause a problem. For example, if a string is not an object, true */S = 'abc'; alert (S. constructor = string); // true // The conditions can be added to solve this problem, but it is better to use the next method directly.

  

Determine the class to which an object belongs (Method 2: Use the instanceof keyword)

 
VaR STR, arr, STR; STR = new string (); alert (STR instanceof string); // truearr = new array (); alert (ARR instanceof array ); // truestr = 'abc'; alert (STR instanceof string); // false/* but this also has a problem, because the object is the ancestor of all classes... */STR = new string (); alert (STR instanceof object); // true

  

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.