Last active
December 11, 2015 01:58
-
-
Save numeroSette/4527068 to your computer and use it in GitHub Desktop.
Javascript OOP - Example
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
var foo = function(){}; | |
foo.prototype.bar = (function() { | |
return 'Test'; | |
}); | |
var obj = new foo(); | |
console.log(obj.bar()); |
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
var foo = (function(){ | |
this.bar = (function() { | |
return 'bar'; | |
}); | |
}); | |
var obj = new foo(); | |
console.log(obj.bar()); |
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
var obj = {}; | |
Object.defineProperty(obj, "foo", { | |
value: "foo", | |
writable: true, | |
enumerable: true, | |
configurable: true | |
}); | |
console.log(obj.foo); |
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
var obj = {}; | |
Object.defineProperties(obj, { | |
"foo": { | |
value: "foo", | |
writable: true, | |
enumerable: true, | |
configurable: true | |
}, | |
"bar":{ | |
value: "bar", | |
writable: true, | |
enumerable: true, | |
configurable: true | |
} | |
}); | |
console.log( obj.foo ); | |
console.log( obj.bar ); |
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
var foo = function(){}; | |
var bar = new foo(); | |
console.log(bar); | |
var foo = {}; | |
console.log(foo); | |
var foo = Object.create(null); | |
console.log(foo); |
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
Foo = {}; | |
(function (bar) { | |
bar.consoleBar = function(){ | |
return ('Jhon Doe'); | |
}; | |
})(Foo); | |
console.log(Foo.consoleBar()); |
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
/* | |
b = (function(){ | |
this.nome = 'a'; | |
}); | |
a = new b(); | |
console.log(a.nome); | |
*/ | |
/* | |
a = (function(){ | |
b = (function(){ | |
this.nome = 'a'; | |
}); | |
return new b(); | |
})(); | |
console.log(a.nome); | |
*/ | |
/* | |
Module = {}; | |
(function(Module){ | |
Module.Namespace = (function(){ | |
this.Class = (function(){ | |
this.Method = (function(){ | |
return 'Foo'; | |
}); | |
}); | |
}); | |
})(Module); | |
a = new (new Module.Namespace()).Class().Method(); | |
console.log(a); | |
*/ | |
/* | |
Module = {}; | |
(function(Module){ | |
Module.Namespace = (function(){ | |
this.Class = (function(){ | |
this.method = (function(){ | |
return 'Foo'; | |
}); | |
}); | |
}); | |
return (Module.Namespace()); | |
})(Module); | |
console.log(Module); | |
console.log(new Module.Class().method()); | |
*/ | |
/* | |
Module = {}; | |
Method = (function(Module){ | |
Module.Namespace = (function(){ | |
this.Class = (function(){ | |
this.method = (function(){ | |
return 'Foo'; | |
}); | |
}); | |
}); | |
return new new Module.Namespace().Class().method(); | |
})(Module); | |
console.log(Module); | |
console.log(Method); | |
*/ | |
/* | |
Module = (function(){ | |
this.Namespace = (function(){ | |
this.Class = (function(){ | |
this.method = (function(){ | |
return 'Foo'; | |
}); | |
}); | |
}); | |
}); | |
console.log(new new new Module().Namespace().Class().method()); | |
*/ | |
/* | |
Module = {}; | |
Namespace = (function(Module){ | |
Module.Namespace = (function(){ | |
this.Class = (function(){ | |
this.method = function(){ | |
return 'Foo'; | |
}; | |
}); | |
}); | |
return (new Module.Namespace()); | |
})(Module); | |
console.log(new Namespace.Class().method()); | |
*/ | |
/* | |
var Namespace; | |
(function (Namespace) { | |
var Class = (function () { | |
function Class(value) { | |
this.attribute = value; | |
} | |
Class.prototype.method = function () { | |
return "Hello, " + this.attribute; | |
}; | |
return Class; | |
})(); | |
Namespace.Class = Class; | |
})(Namespace || (Namespace = {})); | |
*/ | |
/* | |
var __extends = this.__extends || function (d, b) { | |
function __() { this.constructor = d; } | |
__.prototype = b.prototype; | |
d.prototype = new __(); | |
}; | |
var Mamifero; | |
(function (Mamifero) { | |
var Animal = (function () { | |
function Animal(metros) { | |
this.metros = metros; | |
} | |
Animal.prototype.andar = function () { | |
return alert('Andou ' + this.metros + ' metros'); | |
}; | |
return Animal; | |
})(); | |
Mamifero.Animal = Animal; | |
var cachorro = (function (_super) { | |
__extends(cachorro, _super); | |
function cachorro(metros, latido) { | |
_super.call(this, metros); | |
this.latido = latido; | |
} | |
cachorro.prototype.latir = function () { | |
return alert('Latiu ' + this.latido); | |
}; | |
return cachorro; | |
})(Animal); | |
Mamifero.cachorro = cachorro; | |
})(Mamifero || (Mamifero = {})); | |
var pastorAlemao = new Mamifero.cachorro(15, 'Au'); | |
pastorAlemao.andar(); | |
pastorAlemao.latir(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment