The content shared with you in this article is about the native js constructor, which has certain reference value. Friends in need can refer to it
javascript is an object-based language, which contains 5 Native data type:
number (numeric type)
boolean (Boolean type)
string(string type)
null(empty)
undefined(undefined)
other than , everything else is an object, and a function is also an object;
function fn(){ //这是一个函数声明,函数是一个特殊的对象}
Constructor function definition
The constructor is an ordinary function , there is no difference from other functions, it can be understood as function == constructor, it is just a conceptual definition, use it to instantiate objects.
For JavaScript’s built-in objects, Object, Array, Date, etc. are all constructors.
function Fn(){}var f = new Fn() //实例化对象
The object returned by using the new operator is not necessarily the instance itself. You can also use return to change the return value in the constructor:
function Obj() { this.a = 1; return { a: 2 }; }var o = new Obj(); // o并不是Obj的实例console.log(o.a); // 输出2
As mentioned earlier, the function is also an object. In JavaScript Among the built-in objects, all function objects are instances of the Function constructor, such as: Object, Array, etc.
You can use the instanceof operator to verify
#The instanceof operator returns whether a specified object is an instance of a class, in the format: A instanceof B. Among them, the left operand must be an object, and the right operand must be a class (constructor);
Judgment process: If function B is found in the prototype chain of object A, Then the instanceof operator will return true, otherwise it will return false.
alert(Function instanceof Function); // Function函数对象 本身就是自己的一个实例alert(Object instanceof Function); // Object函数对象 是 Function构造函数的一个实例alert(arr instanceof Array);
javascript is an object-based language, which contains 5 native data types:
number( Numeric type)
boolean(Boolean type)
string(String type)
null (empty)
undefined (undefined)
In addition, everything else is an object, and the function is also an object;
function fn(){ //这是一个函数声明,函数是一个特殊的对象}
Constructor function definition
The constructor is an ordinary function, no different from other functions, it can be understood as function == constructor, it It's just a conceptual definition, used to instantiate objects.
For JavaScript’s built-in objects, Object, Array, Date, etc. are all constructors.
function Fn(){}var f = new Fn() //实例化对象
The object returned by using the new operator is not necessarily the instance itself. You can also use return to change the return value in the constructor:
function Obj() { this.a = 1; return { a: 2 }; }var o = new Obj(); // o并不是Obj的实例console.log(o.a); // 输出2
As mentioned earlier, the function is also an object. In JavaScript Among the built-in objects, all function objects are instances of the Function constructor, such as: Object, Array, etc.
You can use the instanceof operator to verify
#The instanceof operator returns whether a specified object is an instance of a class, in the format: A instanceof B. Among them, the left operand must be an object, and the right operand must be a class (constructor);
Judgment process: If function B is found in the prototype chain of object A, Then the instanceof operator will return true, otherwise it will return false.
alert(Function instanceof Function); // Function函数对象 本身就是自己的一个实例alert(Object instanceof Function); // Object函数对象 是 Function构造函数的一个实例alert(arr instanceof Array);
Related recommendations:
Implementation and use of js constructors, index arrays and properties_javascript skills
The above is the detailed content of Native js constructor. For more information, please follow other related articles on the PHP Chinese website!