constructor and prototype in JS

object.constructor : The constructor property of an object refers to the object's constructor.

// For example, an array is created with the Array() constructor, then a.constructor refers to Array: 
a = new Array(1,2,3); // Create an object 
a.constructor == Array // true

The constructor property is often used to determine the type of an unknown object.

1: Given an unknown value, you can use the typeof operator to determine whether it is a primitive value or an object,

2: If it is an object, you can use the constructor attribute to determine the object type

// For example, the function below is used to determine whether a given value is an array 
function isArray(x){
   return (( typeof x == "object") && (x.constructor == Array));  
}

-------------------------------------------- Difference between constructor and prototype- -------------------------------------------------- ----------------------

1 function Person(name){
2   this.name = name;
3   this.showMe = function(){
4       alert(this.name);
5   }    
6 }
7 var one = new Person("tommy");
8 one.showMe();// tommy

According to javascript, the Person defined by the function is an Object, and it is also a very special object. There is an important difference between the object defined by the function and the object generated by the new operator.

The object defined by Function has a prototype property, and the object generated by using new has no prototype property.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324931208&siteId=291194637