Last active
August 29, 2015 13:57
-
-
Save singuerinc/9599996 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
define('Human', [], function() { | |
// static private var | |
var _numEyes = 2; | |
// constructor | |
var Human = function(name) { | |
// public var | |
this.name = name; | |
// pseudo-protected var | |
this._age = 0; | |
}; | |
// instance method | |
Human.prototype.walk = function() { | |
return this.name + ' is walking'; | |
}; | |
Human.prototype.setAge = function(value) { | |
this._age = value; | |
}; | |
Human.prototype.getAge = function() { | |
return this._age; | |
}; | |
Human.prototype.getNumEyes = function() { | |
return _numEyes; | |
}; | |
// class static method | |
Human.GET_TYPE = function() { | |
return 'biped'; | |
}; | |
// class static const | |
Human.NUM_LEGS = 2; | |
return Human; | |
}); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>RequireJS Example</title> | |
</head> | |
<body> | |
<script type="text/javascript" src="bower_components/requirejs/require.js" data-main="js/main"></script> | |
</body> | |
</html> |
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
define('John', ['Human'], function(Human){ | |
var John = function(){ | |
Human.call(this, 'John'); | |
this._age = 28; | |
}; | |
John.prototype = Object.create(Human.prototype); | |
var _super_ = Human.prototype; | |
John.prototype.walk = function() { | |
return _super_.walk.call(this) + ' quickly'; | |
}; | |
return John; | |
}); |
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
require(['Human', 'John'], function(Human, John){ | |
console.log( Human.GET_TYPE() ); // biped | |
console.log( Human.NUM_LEGS ); // 2 | |
var human = new Human('Peter'); | |
console.log( human.name ); // Peter | |
console.log( human.walk() ); // Peter is walking | |
console.log( human.getAge() ); // 0 | |
var john = new John(); | |
console.log( john.name ); // John | |
console.log( john.walk() ); // John is walking quickly | |
console.log( john.getAge() ); // 28 | |
console.log( john.getNumEyes() ); // 2 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment