-
-
Save rlidwka/0ad094386f60f93318bf to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Old and boring way to write classes. | |
* Does not allow you do subclass native classes | |
* (if you subclass an error, it'd just be [object Object]) | |
*/ | |
function Class() { | |
this.foo = 1 | |
} | |
Class.prototype.bar = 2 | |
function SubClass() { | |
Class.call(this) | |
this.subfoo = 3 | |
} | |
require('util').inherits(SubClass, Class) | |
SubClass.prototype.subbar = 4 | |
// checking that all properties are in place | |
for (var i in new SubClass) console.log(i) | |
/* | |
* Cool and useful way to write classes. | |
* Allows you to subclass native classes preserving [[Class]] property | |
* (if you subclass an error this way, it'd be [object Error]) | |
*/ | |
function Class() { | |
var self = {} | |
self.__proto__ = Class.prototype | |
self.foo = 1 | |
return self | |
} | |
Class.prototype.bar = 2 | |
function SubClass() { | |
var self = Class() | |
self.__proto__ = SubClass.prototype | |
self.subfoo = 3 | |
return self | |
} | |
require('util').inherits(SubClass, Class) | |
SubClass.prototype.subbar = 4 | |
// checking that all properties are in place | |
for (var i in new SubClass) console.log(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment