Function like mootools

So, I'm trying to learn javascript by learning how Mootools works internally. I look at these lines specifically:

var Type = this.Type = function(name, object){ if (name){ var lower = name.toLowerCase(); var typeCheck = function(item){ return (typeOf(item) == lower); }; Type['is' + name] = typeCheck; if (object != null){ object.prototype.$family = (function(){ return lower; }).hide(); } } if (object == null) return null; object.extend(this); object.$constructor = Type; object.prototype.$constructor = object; return object; }; //some more code new Type('Type',Type); 

What's going on here?

What is assigned to the object in the constructor instruction at the bottom, bottom, global window object?

Why is it called as a constructor with a new operator when a type function seems to only update the passed object, and not create a new one?

In particular, what is 'this' in the string object.extend (this) ;? is it a global window object if it adds all the key pairs, values โ€‹โ€‹of this object to the Type object?

+3
source share
1 answer

Sheesh, issues that recently seem to be focusing more on the internal functions of mootools.

I will answer as far as I know, since I am not the main developer.

Type in MooTools is quite similar to a class (in fact, the constructor of the class itself is a type ), but it focuses more on data / values โ€‹โ€‹and Value Types. It is also designed to extend the native types defined by the ECMA specification and makes them more flexible.

I think it makes no sense to talk about data types in general ( String , Number , Array , Object , etc.). Why do they need to be expanded? Well, for starters, Duct Typing is a little freaky in js. typeof {}; // object typeof {}; // object , typeof []; // object typeof []; // object , typeof new Date(); // object typeof new Date(); // object , etc. - not the most useful, even if, since all types are inherited from the object and logically combine them, this will not help you write code.

So, in the context of js objects, they are created from constructor objects ...

Which type does not replace constructor functions, but modifies an existing constructor by adding new methods or properties to it.

eg. new Type('Array', Array);

This will turn the built-in Array constructor into an object of type type. You do not need to save the result or anything else - this is a one-time operation that changes the original and leaves it open for manipulation.

typeOf(Array); // type

So what is different? Well, for starters, typeOf([]) can now actually tell us that it is actually an Array . And what actually happens here: object.extend(Type); magic bit. It will copy to the target all the properties defined in the Type object, you can see them here:

https://github.com/mootools/mootools-core/blob/master/Source/Core/Core.js#L211-232

So, right away your newly created type gets all the important implement and extend methods.

More advanced, create a new type based on the built-in array constructor:

 var foo = new Type('foo', Array), a = new foo(); // it a real boy! a.push('hi'); // ['hi'], foo, object console.log(a, typeOf(a), typeof a); 

But what if we need a custom type? Is one of them magical and special? No problem, because argument 2 can indeed be a (anonymous) function.

 var Awesome = new Type('awesome', function(name, surname) { // console.log('IR teh construct0r!'); this.name = name; this.surname = surname; }); // extend it a little. Awesome.implement({ getName: function() { return this.name; }, getSurname: function() { return this.surname; } }); var Dimitar = new Awesome('dimitar', 'christoff'); console.log(typeOf(Dimitar)); // awesome console.log(Dimitar.getName()); // dimitar 

In another example, check out DOMEvent. It takes the above and turns it into a faster, more compact type of object. https://github.com/mootools/mootools-core/blob/master/Source/Types/DOMEvent.js#L21

So why is this not a class? Because classes are more expensive and events happen all the time.

Hope this helps you. for a more detailed explanation, ask on the mailing list and hope for the best, maybe the main developer will have time, because this is not your standard question about how to get a question about the operation of the accordion ...

+3
source

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


All Articles