从必需的文件扩展 Node.js 中的 Array.prototype

发布于 2024-11-02 18:45:46 字数 1449 浏览 0 评论 0原文

我将以下内容保存在 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);

然后,从终端:

> 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2024-11-09 18:45:46

您可以创建一个包含扩展的文件:

array.extensions.js

if(!Array.prototype.Last){
    Array.prototype.Last = function(){
        return this.slice(-1)[0];
    }
}

if(!Array.prototype.First){
    Array.prototype.First = function(){
        return this[0];
    }
}

,然后将此文件包含到您的启动文件中。

app.js:

require('{path}/array.extensions');
var express = require('express');
/* rest of your code */

启动时引用此文件一次就足以使用...

You can create a file which includes your extensions:

array.extensions.js

if(!Array.prototype.Last){
    Array.prototype.Last = function(){
        return this.slice(-1)[0];
    }
}

if(!Array.prototype.First){
    Array.prototype.First = function(){
        return this[0];
    }
}

then include this file to your startup file.

app.js:

require('{path}/array.extensions');
var express = require('express');
/* rest of your code */

referring this file once on startup is enough to use...

北渚 2024-11-09 18:45:46

REPL 中的每个命令都是通过 vm.runInContext 使用共享上下文对象执行的。该对象是在 REPL 初始化时通过复制全局对象中的所有内容来创建的。由于 required 模块只会在将 Array.prototype 复制到上下文对象后才扩展它,因此修改后的版本永远不会公开。

或者至少这是我可以从源代码推断出来的。我对 V8 的内部工作原理一无所知:) 正如您现在可能已经发现的那样,您的示例在 REPL 之外运行良好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文