在一个require'd文件中扩展Node.js中的Array.prototype [英] Extending Array.prototype in Node.js, from a require'd file

查看:65
本文介绍了在一个require'd文件中扩展Node.js中的Array.prototype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在test.js中保存了以下内容。它成功地在浏览器中扩展了Array,但它似乎不适用于node和require。有人可以解释这里有什么问题吗?

 (function(){

Array.prototype.max = function(){
return console.log(Array.prototype.max);
};
return Array.max = function(){
return console.log( Array.max);
};

})。call(this);

然后,从终端:

 >我的MacBook-Pro:me $ node 
> var test = require(./ test)
> [1,2,3] .max()
TypeError:对象1,2,3在[对象上下文]中没有方法'max'
:接口处的1:9
。 ;匿名> (repl.js:171:22)Interface.emit(events.js:64:17)
在Interface._onLine(readline.js:153:10)
在接口处。 _line(readline.js:408:8)
在Interface._ttyWrite(readline.js:585:14)
在ReadStream。< anonymous> (readline.js:73:12)ReadStream.emit(events.js:81:20)
ReadStream._emitKey(tty_posix.js:307:10)
ReadStream
。 onData(tty_posix.js:70:12)
> Array.max()
TypeError:对象函数Array(){[native code]}在[object Context]中没有方法'max'
:接口处的1:7
。<匿名> (repl.js:171:22)Interface.emit(events.js:64:17)
在Interface._onLine(readline.js:153:10)
在接口处。 _line(readline.js:408:8)
在Interface._ttyWrite(readline.js:585:14)
在ReadStream。< anonymous> (readline.js:73:12)ReadStream.emit(events.js:81:20)
ReadStream._emitKey(tty_posix.js:307:10)
ReadStream
。 onData(tty_posix.js:70:12)


解决方案

每一个REPL中的命令通过带有共享上下文对象的 vm.runInContext 执行。通过从全局对象复制所有内容,在REPL初始化时创建此对象。由于require'd模块只会在之后将 Array.prototype 扩展到上下文对象,因此修改后的版本永远不会公开。 / p>

或者至少我可以从源代码。我对V8的内部工作原理一无所知:)正如你现在可能已经发现的那样,你的例子在REPL之外工作正常。


I have the following saved in test.js. It successfully extends Array in the browser, but it doesn't seem to work with node and require. Can someone explain what's wrong here?

(function() {

Array.prototype.max = function() {
  return console.log("Array.prototype.max");
};
return Array.max = function() {
  return console.log("Array.max");
};

}).call(this);

Then, from a terminal:

> My-MacBook-Pro: me$ node
> var test = require("./test")
> [1,2,3].max()
TypeError: Object 1,2,3 has no method 'max'
    at [object Context]:1:9
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)
    at ReadStream.onData (tty_posix.js:70:12)
> Array.max()
TypeError: Object function Array() { [native code] } has no method 'max'
    at [object Context]:1:7
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)
    at ReadStream.onData (tty_posix.js:70:12)

解决方案

Every command in the REPL is executed via vm.runInContext with a shared context object. This object is created at REPL initialization by copying everything from the global object. Since the require'd module will only extend Array.prototype after it has been copied to the context object, the modified version is never exposed.

Or at least that's what I could deduce from the source code. I know nothing about the inner workings of V8 :) And as you probably have found out by now, your example works fine outside the REPL.

这篇关于在一个require'd文件中扩展Node.js中的Array.prototype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆