var Foo = new Class({ Extends: Bar, initialize: function(firstname, lastname) { this.parent(firstname); this.lastname = lastname; }, sayHello: function(){ alert(this.lastname || this.firstname); } });
MyClass = function() {/* constructor */}; MyClass.prototype.firstMethod = function() {/**/}; MyClass.prototype.secondMethod = function() {/**/}; MyClass.prototype.thirdMethod = function() {/**/};
MyObject.prototype
. Do not do without this design, but simply do not repeat it. Inside the library will be all the same MyClass = new Class({ initialize : function() {/* constructor */}, firstMethod : function() {/**/}, secondMethod: function() {/**/}, thirdMethod : function() {/**/} });
instanceof
) MyAnotherClass = function() {/* constructor */}; var Tmp = function() { } Tmp.prototype = MyClass.prototype MyAnotherClass.prototype = new Tmp() MyAnotherClass.prototype.constructor = MyAnotherClass;
MyAnotherClass = new Class({ Extends: MyClass });
var MyClass = function() { /* */ this.firstMethod = function() {/**/}; this.secondMethod = function() {/**/}; this.thirdMethod = function() {/**/}; /* , */ }; var MyAnotherClass = function() { // MyClass.apply(this, arguments); this.elseOneMethod = function() {/**/}; }; var myAC = new MyAnotherClass(); console.log(myAC instanceof MyClass); // false
instanceof
works, in which functions are created every time an object is created, which is why the memory flows like a river and the processor cries. Instead of creating a function in one place and giving it a link, we will create it every time. These functions are not present in the prototype (and in embedded objects such as Array, Number and the rest everything is exactly in the prototype), they cannot be redefined or obtained out of context, they are incorrectly determined using hasOwnProperty
and work prohibitively slowly (hundreds of times): var MyClass = function() { this.method1 = function(){}; }; MyClass.prototype.method2 = function(){}; var myClass = new MyClass; console.log(myClass.hasOwnProperty('method1')); // true console.log(myClass.hasOwnProperty('method2')); // false var Foo = function() { this.method1 = function(){}; this.method2 = function(){}; this.method3 = function(){}; this.method4 = function(){}; this.method5 = function(){}; this.method6 = function(){}; this.method7 = function(){}; this.method8 = function(){}; this.method9 = function(){}; } var Bar = function() {}; Bar.prototype.method1 = function(){}; Bar.prototype.method2 = function(){}; Bar.prototype.method3 = function(){}; Bar.prototype.method4 = function(){}; Bar.prototype.method5 = function(){}; Bar.prototype.method6 = function(){}; Bar.prototype.method7 = function(){}; Bar.prototype.method8 = function(){}; Bar.prototype.method9 = function(){}; /** * Chrome 8 * Foo: 260 * Bar: 26 * Firefox 3.5 * Foo: 22081 * Bar: 158 */ console.time('Foo'); for (var i = 1000000; i--;) new Foo(); console.timeEnd('Foo'); console.time('Bar'); for (var i = 1000000; i--;) new Bar(); console.timeEnd('Bar');
instanceof
does not work in childrenJS preach prototype OOP, insert class crutches - not gut. Learn the language and its capabilities.
Source: https://habr.com/ru/post/111393/
All Articles