javascript - node.js, require() and prototypal-inheritance -
i'm pretty new node.js, , javascript well, feel free point out seems awkward. i'm here learn.
here's i'm trying do:
- "john" object should inherit "client" object, including functions
- use object.create() instead of "new" keyword in instantiating new objects , inheritance
here single file test worked:
var sys=require('sys'); var client = { ip: null, stream : null, state: "connecting", eyes: 2, legs: 4, name: null, tostring: function () { return this.name + " " + this.eyes + " eyes , " + this.legs + " legs, " + this.state + "."; } } var john = object.create(client, { name: {value: "john", writable: true}, state : {value: "sleeping", writable: true} }); sys.puts(john); // john 2 eyes , 4 legs, sleeping here happens when split different files:
---- client.js ----
module.exports = function (instream, inip){ return { ip: inip, stream : instream, state: "connecting", eyes: 2, legs: 4, name: null, tostring: function () { return this.name + " " + this.eyes + " eyes , " + this.legs + " legs, " + this.state + "."; }, }; }; ---- john.js ----
var client = require("./client"); module.exports = function (inname, instate){ return object.create(client, { state : {value: inname, enumerable: false, writable: true}, name: {value: instate, enumerable: true, writable: true}, }); }; ---- main.js ----
var sys = require("util"); var client = require("./client")("stream","168.192.0.1"); sys.puts(client); // null 2 eyes , 4 legs, connecting var john = require("./john")("john","sleeping"); sys.puts(john); //function.prototype.tostring no generic sys.puts(sys.inspect(john)); // { name: 'sleeping' } sys.puts(sys.inspect(john, true)); // {name: 'sleeping', [state]: 'john'} questions:
- what doing wrong in splitting files , in use of require() that's causing problems?
- why john object have 'sleeping' name , 'john' state? know it's order put lines, should not follow parameters i've put in constructor?
- is there better way of doing this? i'm inclined learn object.create() rather rely on "new" keyword.
on #2:
return object.create(client, { state : {value: inname, enumerable: false, writable: true}, name: {value: instate, enumerable: true, writable: true}, }); that's simple mistyping on side swapping inname , instate.
on #1:
i suspect slight difference between original single-file code , new 3 file code @ fault. particular comment caught eye on mdn's page on object.create, specifically,
of course, if there actual initialization code in constructor function, object.create cannot reflect it
your client.js file producing constructor function. meant write in john.js (combining #1 , #2):
return object.create(client(), { state : {value: instate, enumerable: false, writable: true}, name: {value: inname, enumerable: true, writable: true}, }); call client function returns object rather function, , create new object built on top of that.
on #3:
i don't see why couldn't use pattern, have demonstrated is:
- foreign how javascript objects created today (though no doubt faster construction mechanism).
- using newer syntax (that drops
new) syntax errors don't "jump out" @ old, java-style syntax.
just keep in mind. i'm glad asked question, though, because know object.create. :)
Comments
Post a Comment