call/apply
keywords, var obj = new Object(); // var obj = {}; // .
obj.name = 'Victor'; // .property obj['name']='Victor'; //
console.log(obj.name); // .property console.log(obj['name']); //
var obj = { name : 'Viktor', age : 32 };
new
keyword.
var Donkey = function(){ //… }; // «» var iaDonkey = new Donkey();
iaDonkey = new Object(); // .
aDonkey.constructor == Donkey // true
iaDonkey instanceof Donkey // true
iaDonkey.__proto__ = Donkey.prototype
var iaDonkey = function(){ this.constructor(); // function Donkey() // … };
// , : function New (F, args) { /*1*/ var n = {'__proto__': F.prototype}; /*2*/ F.apply(n, args); /*3*/ return n; }
prototype
value to proto
.apply
.New
. for (var i = 0; i < 10; i++) { setTimeout(function () { console.log(i); }, 0); }
setTimeout
will begin.
for (var i = 0; i < 10; i++) { (function (m) { setTimeout(function () { console.log(m); },0); })(i) }
i. m
i. m
will try to get the value from the closest upper scope through the closures. And since we passed it through a self-calling function, it will each time be equal to its value (the value of the variable i
), and we will get from 0 to 9 10 times.
BarChart
, it has public methods - to build a graph and update its values. There are private methods and properties - what we need to do without showing it to the world around us.
BarChart
via new
, it turns out that this is a constructor function, and we will create a new object of this class. But all private methods will close in this variable, and we will be able to access them inside. They will remain in the scope that this function will create.
apply
construction:
var obj = { outerWidth: 'pliers' }; function getWidth(){ return this.outerWidth; } var a = getWidth(); var b = getWidth.apply(obj); console.log(a); // , this windows console.log(b); // pliers. outerWidth — windows, , , windows.outerWidth
Calling func.call(context, a, b...)
func(a, b...), but this == context.
call(context, param1, param2 …) apply(context, [args])
function: function(args) – this == window method: obj.funct(args) – this == obj Apply: func.apply(obj,args) – this == obj Constructor: new func(args) – this == new object
RouteHistorical
object and the same arguments to the base class via the context. The method initializes all fields, and we get a new object.
RouteHistorical
heir to the base filter. In line 81, we write a reference to the base constructor class in the prototype
property. The prototype
method is completely overwritten, and the constructor is lost. When we create a new object, it will not know what to turn to.
prototype.constructor
property to a link to itself. A property of the constructor
class always refers to itself.
prototype
property makes sense paired with the new
keyword. When we create a new object, we write the prototype
value in the __proto__
property. It contains a reference to the class that is the parent for our class.
prototype
only needed to say what needs to be written in __proto__
when instantiating a new object.
// unsafe var filter = { EnablePagination: true }; function BaseFilter(size) { this.PageSize = size; this.__proto__ = filter; } // safe var filter= { EnablePagination: true }; function BaseFilter(size) { this.PageSize = size; } BaseFilter.prototype = filter;
__proto__
is considered unsafe to directly access __proto__
, and not all browsers allow it.
extend
function:
function extend(Child, Parent) { var F = function() { } F.prototype = Parent.prototype // Child.prototype = new F() // Child __proto__ prototype Child.prototype.constructor = Child // , . Child.superclass = Parent.prototype // Parent };
function BaseFilterModel(..) { ... } function RouteHistoricalFilterModel(..) { ... }
function isInstanceOf(obj, constructor) { if (obj.__proto__ === constructor.prototype) { return true; } else if (obj.__proto__ !== null) { return isInstanceOf(obj.__proto__, constructor) } else { return false } };
new
keyword.
__proto__
, and so on throughout the chain. Thus, inheritance is implemented in JavaScript.
fn.__proto__
stores the link to fn.prototype
.
new
operator creates an empty object with the only property __proto__
that refers to F.prototype
. The constructor executes F
, where this
context is a previously created object, sets its properties and returns this object.
instanceof
operator does not check that the object was created through the ObjectsConstructor
constructor, but makes a decision based on the entire prototyping chain.
this
depends on the context, i.e. on how we call the function.Source: https://habr.com/ru/post/302518/