Created
March 10, 2017 15:13
-
-
Save briedis/ded59c04b7682b79548ba307bc52d06d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ // namespace (ignore) | |
// bez priv var | |
var Simple = Class( | |
/** | |
* @lends {Simple#} | |
*/ | |
{ | |
__NAME:'Simple', | |
prop:123, | |
arrayOrObject:[ 1, 2, 3 ], // var droši šādi darīt instancē būs kopija | |
obj:new Test(args), // arī klases var izmantot ka propertijus | |
method:function(){ | |
} | |
} | |
); | |
/** | |
* @class | |
*/ | |
var TestBase = Class(function(){ | |
var _privVar = 0; // private | |
/** | |
* @lends {TestBase#} | |
*/ | |
return { | |
__NAME:'TestBase', | |
__construct:function(){ // konstruktors bez argumentiem tiks izsaukts automātiski, ja ekstendos | |
_privVar++; | |
}, | |
method:function(){ | |
} | |
}; | |
}); | |
/** | |
* @class | |
* @extends TestBase | |
*/ | |
var Test = Class( function(){ | |
/** @type {TestBase} */ | |
var PARENT; // šis, lai IDE saprastu | |
/** | |
* @lends {Tests#} | |
*/ | |
return { | |
__NAME:'Test', | |
method:function(){ | |
PARENT.method(); | |
} | |
}; | |
}, TestBase ); | |
var base = new TestBase; | |
var child = new Test; | |
if( child instanceof TestBase ){} // true | |
if( child instanceof Test ){} // true | |
if( base instanceof Test ){} // false | |
/** | |
* @class | |
*/ | |
var Test2 = Class( function(){ | |
var _interval; | |
/** | |
* @lends {Test2#} | |
*/ | |
return { | |
__NAME:'Test2', | |
count:0, | |
_tick:function(){ | |
console.log( ++ this.count ); | |
}, | |
run:function(){ | |
clearInterval(_interval); | |
_interval = setInterval( this._tick, 1000 ); // D.closure nav jālieto | |
}, | |
stop:function(){ | |
clearInterval(_interval); | |
} | |
}; | |
} ); | |
var test = new Test2; | |
var run = test.run; | |
run(); // arī te closure nav jālieto | |
})(); | |
/* | |
1) normāli konstruktori | |
2) extendošana | |
3) vienmēr closure - metodi var variablī ieglavāt, bet this saglabājas | |
4) priv. var | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment