Skip to content

Instantly share code, notes, and snippets.

@majgis
Last active October 11, 2015 06:27
Show Gist options
  • Save majgis/3816953 to your computer and use it in GitHub Desktop.
Save majgis/3816953 to your computer and use it in GitHub Desktop.
Javascript instantiable class experiments.
// Modified excerpt from:
// https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript
// define the Person Class
function Person(name) {
//Public property
this.name = name;
//Private property
this._whatever = ''
this.sayHello()
}
//Static method
Person.something = function(){
}
//Public property
Person.prototype.somethingElse
//Private property
Person.prototype._KNOWN = {'mike':true, 'bob':true}
//Private method
Person.prototype._recognize = function(){
return this._KNOWN[this.name];
};
//Public method
Person.prototype.sayHello = function(){
if (this._recognize()){
alert('hello ' + this.name);
}
};
>p = new Person('mike')
(alert)
>p = new Person('fred')
(no alert)
// Modified excerpt from:
// https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript
// define the Person Class
function Person() {}
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = Object.create(Person.prototype); //A common mistake is to use new Person(); use es5shim if < ie8 for Object.create
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment