Created
June 13, 2015 21:17
-
-
Save NoMan2000/9ba732499351759cc026 to your computer and use it in GitHub Desktop.
Creating an object in javaScript: The Pain
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
// Code goes here | |
(function testInheritance(global, doc, $) { | |
"use strict"; | |
var MyFunc = Object.create({}, { | |
_foo: { | |
value: null, | |
writable: true, | |
enumerable: true | |
}, | |
_newfoo: { | |
value: null, | |
writable: true, | |
enumerable: true | |
}, | |
_sub: { | |
value: document.getElementById('sub'), | |
writable: true, | |
enumerable: true | |
}, | |
_myDone: { | |
value: function myDone() { | |
this.sub.innerHTML = "Hell yeah"; | |
}, | |
writable: true, | |
enumerable: true | |
}, | |
_myFail: { | |
value: function myFail() { | |
console.log(this); | |
}, | |
writable: true, | |
enumerable: true | |
}, | |
_activate: { | |
value: function() { | |
$.mockjax({ | |
url: "fake.php", | |
responseTime: 0, | |
responseText: 'A text response from mock' | |
}); | |
$.ajax({ | |
url: "fake.php", | |
type: "POST", | |
data: { | |
foo: this.foo, | |
newfoo: this.newfoo | |
} | |
}).done(this.myDone) | |
.fail(this.myFail); | |
}, | |
writable: true, | |
enumerable: true | |
}, | |
foo: { | |
get: function() { | |
return this._foo; | |
}, | |
set: function(value) { | |
this._foo = value; | |
} | |
}, | |
newfoo: { | |
get: function() { | |
return this._newfoo; | |
}, | |
set: function(value) { | |
this._newfoo = value; | |
} | |
}, | |
sub: { | |
get: function() { | |
return this._sub; | |
}, | |
set: function(value) { | |
this._sub = value; | |
} | |
}, | |
myDone: { | |
get: function() { | |
this._myDone(); | |
}, | |
set: function(value) { | |
if (typeof value !== "function") { | |
return null; | |
} | |
this._myDone = value; | |
} | |
}, | |
myFail: { | |
get: function() { | |
this._myFail(); | |
}, | |
set: function(value) { | |
if (typeof value !== "function") { | |
return null; | |
} | |
this._myFail = value; | |
} | |
}, | |
activate: { | |
get: function() { | |
this._activate(); | |
}, | |
set: function(value) { | |
if (typeof value !== "function") { | |
return null; | |
} | |
this._activate = value; | |
} | |
} | |
}), | |
testFunc = Object.create(MyFunc); | |
testFunc.foo = "boo"; | |
// console.log(testFunc); | |
// testFunc.foo = "This is a string!"; | |
testFunc.activate; | |
}(window, document, jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment