// var cat = {}; // : cat.name = "Garfield"; // : cat.getName = function() { return cat.name; };
// : var cat = {name: "Garfield"}; // -: var cat = new Object(); cat.name = "Garfield";
Object
constructor forces the interpreter to check whether this function is redefined in the local context.Object
constructor. But we all know that sometimes you have to use some old code, and in this case it is useful to know about one feature of this constructor. It takes an argument, and depending on its type, can assign the creation of an object to another constructor built into the language; in the end, we get the wrong object that we expected: // var o = new Object(); o.constructor === Object; // true var o = new Object(1); o.constructor === Number; //true var o = new Object("string"); o.constructor === String; //true // , : typeof o.substring; // "function"
Object
constructor can lead to unexpected results if we pass to it a value that is unknown at run time.Object
constructor. var cat = new Cat("Garfield"); cat.say(); // "I am Garfield"
var Cat = function(name) { this.name = name; this.say = function() { return "I am" + this.name; } }
new
operator inside a function, the following happens:this
variable; this object inherits the function prototype;this
;this
is returned implicitly at the end of the function (since we did not return anything explicitly).say()
method is added to the this
object. This is not very good, because each time new function new Person()
is called, a new Person()
function will be created in memory. Since the say()
method is the same for all objects created using this constructor, it is better to add it to the Cat
prototype: Cat.prototype.say = function() { return "I am " + this.name; };
this
object, implicitly created in the constructor, is empty: it inherits from the Cat
prototype, but the consideration of prototypes is beyond the scope of this article.new
operator, the constructor always returns an object. By default, this is the object referenced by this
. The constructor returns this
implicitly, but we can explicitly return any other object, for example: var Cat = function() { this.name = "I am Garfield"; var that = {}; that.name = "I am Cat-woman"; return that; }; var cat = new Cat(); cat.name // "I am Cat-woman"
false
, this will not result in an error, but the return statement will be ignored, and the constructor will return this
.new
operator. What happens if you forget this operator? The interpreter will not issue warnings, but this will lead to logical errors. The variable this
will point not to the object inherited from the prototype of the constructor, but to the global object ( window
in the case of the browser): function Cat() { this.name = "Garfield"; } // var cat = new Cat(); typeof cat; // "object" cat.name; // "Garfield" // new: var cat = Cat(); typeof cat; // "undefined" window.name; // "Garfield"
this
standard in this case will not point to a global object. Let's see how to avoid this error if ECMAScript 5 is not available.myFunction()
), and the constructor functions start with the capital one ( MyConstruction()
). Unfortunately, this method almost does not save anything. function Cat() { var that = {}; that.name = "Garfield"; return that; }
me
or self
or whatever you want. function Cat() { return { name: "Garfield" }; }
var first = new Cat(), second = Cat(); first.name; // "Garfield" second.name; // "Garfield"
Cat
will not be available to the objects created with its help.this
in the body of the constructor is an instance of this constructor itself, and if not, call itself again, but this time with the new
operator. It sounds scary, but it's really simple: function Cat() { if (!(this instanceof Cat)) { return new Cat(); } this.name = "Garfield"; } Cat.prototype.meow = "Meow!"; var first = new Cat(), second = Cat(); first.name; // "Garfield" second.name; // "Garfield" first.meow; // "Meow!" second.meow; // "Meow!"
arguments.callee
instead of the constructor name: if (!(this instanceof arguments.callee)) { return new arguments.callee(); }
arguments
object is created that contains all the parameters passed to the function at the time of the call. The callee
property of this object indicates the function being called. But here, you need to be careful: strict ECMAScript 5 mode causes a TypeError
exception when accessing this property, so you should make a choice in advance between the convenience of refactoring and the bright tomorrow.Source: https://habr.com/ru/post/119391/
All Articles