Skip to content

Instantly share code, notes, and snippets.

@singuerinc
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singuerinc/9599996 to your computer and use it in GitHub Desktop.
Save singuerinc/9599996 to your computer and use it in GitHub Desktop.
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;
});
<!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>
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;
});
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