Any function in javascript can be called a constructor as long as it is called with the new keyword.
When using the new keyword to call a function, an object will be implicitly declared inside the function, and then the object will be assigned to this, and finally this will be returned implicitly. When using the new keyword to call the author's book method, it is equivalent to The following process.
function Book(id, bookname){
var o = new Object();
o.id = id;
o.bookname = bookname;
return o;
}
var b = Book(123,'javascript高级程序设计');
The o here is actually the this object we usually see.
There are no classes or constructors in JavaScript.
function Book (){}
// Book 是一个函数
typeof Book
"function"
// Book 是 Function
Book instanceof Function
true
// Book 是 Object
Book instanceof Object
true
Use new to create objects
var book = new Book()
// book 是一个对象
typeof book
"object"
// book 不是 Function 的实例
book instanceof Function
false
// book 是 Object 的实例
book instanceof Object
true
// book 是 Book 的实例
book instanceof Book
true
Although there is no constructor in js, this word is also used in the MDN documentation: https://developer.mozilla.org...
When the code new foo(...) is executed:
A new object is created. It inherits from foo.prototype.
Constructor foo is executed. When executing, the corresponding parameters will be passed in, and the context (this) will be designated as this new instance. new foo is equivalent to new foo(), and can only be used without passing any parameters.
If the constructor returns an "object", then this object will replace the entire new result. If the constructor does not return an object, then the result of new is the object created in step 1. ps: Generally, the constructor does not return any value, but if the user wants to override this return value, he or she can choose to return an ordinary object. cover. Of course, returning an array will also be overwritten, because arrays are also objects.
Even if ES6 adds classes, they are just syntactic sugar:
Actually, there should not be the term ‘constructor’ in js, but should be understood as the construction method of a function. This means that any function can be called new, and any function can be called a ‘constructor’. When you wrote the so-called "constructor", did you notice any difference between it and ordinary functions? No, they are just ordinary functions.
Any function in javascript can be called a
constructor
as long as it is called with thenew
keyword.When using the new keyword to call a function, an object will be implicitly declared inside the function, and then the object will be assigned to this, and finally this will be returned implicitly. When using the new keyword to call the author's book method, it is equivalent to The following process.
The o here is actually the this object we usually see.
There are no classes or constructors in JavaScript.
Use
new
to create objectsAlthough there is no constructor in js, this word is also used in the MDN documentation: https://developer.mozilla.org...
Even if ES6 adds classes, they are just syntactic sugar:
You misunderstood the concept of constructor.
Actually, there should not be the term ‘constructor’ in js, but should be understood as the construction method of a function. This means that any function can be called
new
, and any function can be called a ‘constructor’. When you wrote the so-called "constructor", did you notice any difference between it and ordinary functions? No, they are just ordinary functions.Above, except es6 arrow function.
Every function in js is equivalent to a constructor (except for the true ES6 arrow function).
Hey, amazing js!