带有实例方法的Javascript Array.prototype.map函数

带有实例方法的Javascript Array.prototype.map函数,javascript,arrays,Javascript,Arrays,我想知道是否可以将实例方法传递到Array.prototype.map函数中 nodes.map(n=>n.isBig()) 例如,我基本上想要这个功能,而不需要做一个单独的功能 功能节点(n){ 这个.val=n; } Node.prototype.isBig=函数(){ 返回此值。val>5; } var myArray=[ 新节点(1)、新节点(2)、新节点(3), 新节点(7)、新节点(8)、新节点(9) ]; log(myArray); var result=myArray.

我想知道是否可以将实例方法传递到Array.prototype.map函数中

nodes.map(n=>n.isBig())
例如,我基本上想要这个功能,而不需要做一个单独的功能

功能节点(n){
这个.val=n;
}
Node.prototype.isBig=函数(){
返回此值。val>5;
}
var myArray=[
新节点(1)、新节点(2)、新节点(3),
新节点(7)、新节点(8)、新节点(9)
];
log(myArray);
var result=myArray.map(函数(节点){
返回node.isBig();
});

控制台日志(结果)我不认为在不创建新函数的情况下就可以严格执行,但可以通过现有函数避免函数表达式和声明

例如,您可以使用适当的
this
值调用
节点.prototype.isBig

var result = myArray.map(Function.prototype.call.bind(Node.prototype.isBig));
此外,还有一项提议,我认为这将是允许的

var result = myArray.map(::Node.prototype.isBig.call);
注意:附加参数(索引和数组)将传递给
isBig
方法

功能节点(n){
这个.val=n;
}
Node.prototype.isBig=函数(){
返回此值。val>5;
};
var myArray=[
新节点(1)、新节点(2)、新节点(3),
新节点(7)、新节点(8)、新节点(9)
];
var result=myArray.map(Function.prototype.call.bind(Node.prototype.isBig));

document.write(JSON.stringify(result))我不认为在不创建新函数的情况下就可以严格执行,但可以通过现有函数避免函数表达式和声明

例如,您可以使用适当的
this
值调用
节点.prototype.isBig

var result = myArray.map(Function.prototype.call.bind(Node.prototype.isBig));
此外,还有一项提议,我认为这将是允许的

var result = myArray.map(::Node.prototype.isBig.call);
注意:附加参数(索引和数组)将传递给
isBig
方法

功能节点(n){
这个.val=n;
}
Node.prototype.isBig=函数(){
返回此值。val>5;
};
var myArray=[
新节点(1)、新节点(2)、新节点(3),
新节点(7)、新节点(8)、新节点(9)
];
var result=myArray.map(Function.prototype.call.bind(Node.prototype.isBig));

document.write(JSON.stringify(result))
您必须创建一个以node作为参数的函数(也就是说,它不依赖于
这个

Node.isBig = function(node) {
  return node.isBig();
};

//works because function doesn't depend on this
myArray.map(Node.isBig);
// doesn't work, depends on this
myArray.map(Node.prototype.isBig);
 function passFirstArgAsThis (fun) {
     return function() {
         fun.apply(arguments[0], arguments);
     }
 }
 myArray.map( passFirstArgAsThis(Node.prototype.isBig));
或者,您可以创建一个函数,该函数返回另一个函数,该函数接受其第一个参数并将其作为
this

Node.isBig = function(node) {
  return node.isBig();
};

//works because function doesn't depend on this
myArray.map(Node.isBig);
// doesn't work, depends on this
myArray.map(Node.prototype.isBig);
 function passFirstArgAsThis (fun) {
     return function() {
         fun.apply(arguments[0], arguments);
     }
 }
 myArray.map( passFirstArgAsThis(Node.prototype.isBig));
下面是一个稍微神秘一点的实现

function passFirstArgAsThis(fn) {
    return function() {
        return fn.call.apply(fn, arguments);  
    };
}
ES6中的等效项更具可读性

function passFirstArgAsThis(fn) {
    return function() {
        // arguments[0] will be the first argument passed to call
        // map will pass it as the node, we will make it become `this`
        // when calling the passed in function
        return fn.call(...arguments); 
    };
}
理解了这一点,就更容易理解了

function passFirstArgAsThis(fn) {
    // we return a bound function that will execute the call method, bound
    // to the same passed in function 
    return fn.call.bind(fn);
    // Same as 
    // return function() {
    //    return fn.call(...arguments);
    // }
}
就个人而言,为了可读性,我会使用胖箭头函数,而不需要额外的“静态”函数

nodes.map(n=>n.isBig())

您必须创建一个以node为参数的函数(也就是说,它不依赖于
this

Node.isBig = function(node) {
  return node.isBig();
};

//works because function doesn't depend on this
myArray.map(Node.isBig);
// doesn't work, depends on this
myArray.map(Node.prototype.isBig);
 function passFirstArgAsThis (fun) {
     return function() {
         fun.apply(arguments[0], arguments);
     }
 }
 myArray.map( passFirstArgAsThis(Node.prototype.isBig));
或者,您可以创建一个函数,该函数返回另一个函数,该函数接受其第一个参数并将其作为
this

Node.isBig = function(node) {
  return node.isBig();
};

//works because function doesn't depend on this
myArray.map(Node.isBig);
// doesn't work, depends on this
myArray.map(Node.prototype.isBig);
 function passFirstArgAsThis (fun) {
     return function() {
         fun.apply(arguments[0], arguments);
     }
 }
 myArray.map( passFirstArgAsThis(Node.prototype.isBig));
下面是一个稍微神秘一点的实现

function passFirstArgAsThis(fn) {
    return function() {
        return fn.call.apply(fn, arguments);  
    };
}
ES6中的等效项更具可读性

function passFirstArgAsThis(fn) {
    return function() {
        // arguments[0] will be the first argument passed to call
        // map will pass it as the node, we will make it become `this`
        // when calling the passed in function
        return fn.call(...arguments); 
    };
}
理解了这一点,就更容易理解了

function passFirstArgAsThis(fn) {
    // we return a bound function that will execute the call method, bound
    // to the same passed in function 
    return fn.call.bind(fn);
    // Same as 
    // return function() {
    //    return fn.call(...arguments);
    // }
}
就个人而言,为了可读性,我会使用胖箭头函数,而不需要额外的“静态”函数

nodes.map(n=>n.isBig())

正如你从Oriol的答案中所看到的,它是可以做到的,但它几乎和匿名函数一样详细,而且并不清楚。正如你从Oriol的答案中所看到的,它是可以做到的,但它几乎和匿名函数一样详细,而且不清楚。我正要建议你的
节点。isBig
推荐,但你打败了我但是正如你在评论中提到的,我只会使用Clarity的匿名函数,在我考虑将其定义为“静态”之前,我就这样写过方法。如果我需要从多个位置执行此操作,我将使用
Node.isBig
。我正要建议您的
Node.isBig
建议,但您抢先提出了建议。但是正如您在评论中提到的,我只会使用Clarity的匿名函数。在我考虑将其定义为“静态”之前,我编写了此函数方法。如果我需要从多个位置执行此操作,我会使用
Node.isBig
。这确实很酷,但我已经使用JavaScript 15年了,我总是要阅读类似的几行代码times@JuanMendes是的,我必须在发布前测试它,因为我不确定我是否认为它是正确的。这真的很酷,但我已经做到了n使用JavaScript 15年了,我总是要读几行这样的文字times@JuanMendes是的,我必须在发布之前测试它,因为我不确定我是否认为它是正确的。