Valid Object Constructors

which of the following valid object constructors?

1) var m = function(){}
2) m = function(){}
3) m.prototype.constructor = function(){}
+3
source share
3 answers

They all seem to be valid operators that declare an empty function and assign it to various variables.

Each function in Javascript is both the object itself (or f.prototype does not work) and the potential constructor of the object. Any function can be called with syntax new Thingy(or, perhaps, new min your example). Or it could be called normal - the only special thing newdoes is set thisto an object obtained from f.prototype.

prototype, ({}), , constructor, ( )

, :

var m = function(){};
m.prototype.constructor == m;
+1

var m = {};

.

+1

1) var m = function(){}

2) m = function(){}

Almost the same, except the first, creates a local function with a region, the second - global. And to create an object, you can simply

var obj = new m();
0
source

Source: https://habr.com/ru/post/1756878/


All Articles