node.js - what is the different between obj.__proto__ = events.EventEmitter.prototype and obj.prototype.__proto__ = events.EventEmitter.prototype -


obj.prototype.__proto__ = events.eventemitter.prototype

i have seen code above sometimes, , google it, line copy of eventemitter properties obj. , see code this:

obj.__proto__ = events.eventemitter.prototype

so wondering if same?


i saw first usage in article, in author gives exapmle:

var events = require('events'); function door(colour) {   this.colour = colour;   events.eventemitter.call(this);   this.open = function()   {     this.emit('open');   } } door.prototype.__proto__ = events.eventemitter.prototype; var frontdoor = new door('brown');   frontdoor.on('open', function() {   console.log('ring ring ring'); });  frontdoor.open(); 

and explains:

this line: door.prototype.__proto__ = events.eventemitter.prototype; copies of eventemitter properties door object.

as second way, saw in source of hexo, in init.js, there code:

    var hexo = global.hexo = {     base_dir(){return basedir},     public_dir(){return basedir + 'public/'},     source_dir(){return basedir + 'source/'},     theme_dir(){return basedir + 'themes/' + config.theme + '/'},     plugin_dir(){return basedir + 'node_modules/'},     script_dir(){return basedir + 'scripts/'},     scaffold_dir(){return basedir + 'scaffolds/'},     core_dir(){return path.dirname(dirname) + '/'},     lib_dir(){return dirname + '/'},     version(){return version},     env(){return env},     safe(){return safe},     debug(){return debug},     config(){return config},     extend(){return extend},     render(){return render},     util(){return util},     call(){return call},     i18n(){return i18n.i18n},     route(){return route},     db(){return db}   };    hexo.cache = {};    // inherits eventemitter   hexo.__proto__ = eventemitter.prototype;    // emit "exit" event when process exit   process.on('exit', function(){     hexo.emit('exit');   }); 

the statements not same.

rather obj.prototype.__proto__ = events.eventemitter.prototype, i'd expect see constructor.prototype.__proto__ = events.eventemitter.prototype, (where constructor kind of constructor function, have name. they're capitalized.) because prototype property typically available on functions, , not have special meaning when defined property of regular (non-function) object.

in other words, obj in first line of code given should (constructor) function make sense, , it's uncommon see function have such generic variable name obj.

if you'd share source find exact first statement, may clear things up.


the second example simplest. there's no constructor involved. hexo plain object created object-literal. author wants eventemitter methods available through hexo method, assigns eventemitter.prototype __proto__ property, changes prototype of hexo.

the first code example more complex. here author wants ensure objects constructed door function provide access eventemitter methods. object constructed door function door.prototype prototype. particular prototype has eventemitter prototype, eventemitter functions accessible going 2 steps prototype chain.

"copies of eventemitter properties door object." - particular comment misleading. no properties copied. happens this.

door = new door door.on("open", function() { console.log("door has opened")}) 

the prototype of door door.prototype. if property (in case on) not found when trying access it, js engine @ prototype. prototype of door - door.prototype - has no on defined either, js engine see if door.prototype has prototype. does, in form of events.eventemitter.prototype. , object have on property defined.

hope makes things more clear. javascript prototypical inheritance quite tricky.

see confusion setting something.prototype.__proto__


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -