Created
January 10, 2016 20:39
-
-
Save hsuh/25a08169723a455ab0fb to your computer and use it in GitHub Desktop.
Mucking about with javascript objects
This file contains hidden or 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 a = function(y) { | |
var z = 1; //shared between instances, accessible via prototype method | |
var x = 2; //shared between instances | |
var self = this; | |
this.y = null; //not shared | |
var impl = function(y) { | |
}; | |
var init = function(y) { | |
if (y !== undefined) {self.y = y} | |
return impl; | |
} | |
impl.gety = function() { | |
return self.y; | |
}; | |
impl.sety = function(y) { | |
self.y = y; | |
} | |
impl.getx = function() { | |
return x; | |
}; | |
impl.prototype.getz = function() { | |
return z; | |
}; | |
return init(y); | |
} | |
var b = new a(45); | |
var c = new a(); | |
var d = new a(10); | |
var p = Object.create(b); | |
p.foo = 242; | |
console.log('p x', p.getx()) | |
console.log('p y', p.gety()) | |
console.log('p z', p.prototype.getz()) | |
console.log('=================='); | |
var q = Object.create(p); | |
console.log('q z', q.prototype.getz()) | |
console.log('q foo', q.foo) | |
console.log('=================='); | |
console.log('y from b', b.gety()); | |
console.log('y from c', c.gety()); | |
console.log('y from d', d.gety()); | |
console.log('=================='); | |
console.log('x from b', b.getx()); | |
console.log('x from c', c.getx()); | |
console.log('=================='); | |
console.log('z from b', b.prototype.getz()); | |
console.log('==================') | |
b.sety(100); | |
console.log('y from b', b.gety()); | |
console.log('y from c', c.gety()); | |
console.log('=================='); | |
/*var Request = function() { | |
// Convenience helpers | |
this.endpoints = { | |
user: 'user', | |
login: 'user/login' | |
}; | |
this.apiBase = 'http://myserver.com/api/'; | |
this.make = function(options) { | |
var url = this.apiBase; | |
// resolve URL | |
if(this.endpoints.hasOwnProperty(options.endpoint)) { | |
url += this.endpoint; | |
} | |
// return a new request object | |
return new HTTP(url, options); | |
} | |
console.log('this', this); | |
return this; | |
}; | |
var r = new Request();*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment