Javascript 方法上的Function.prototype不';不要传递对象的此属性
首先,很抱歉我的英语不好 我想为函数类创建一个原型,如: (延迟就是一个例子) 我已经应用了它,当我从函数调用它时,它工作得很好:Javascript 方法上的Function.prototype不';不要传递对象的此属性,javascript,typescript,lodash,Javascript,Typescript,Lodash,首先,很抱歉我的英语不好 我想为函数类创建一个原型,如: (延迟就是一个例子) 我已经应用了它,当我从函数调用它时,它工作得很好: console.log('render'); console.log.delay(1000, 'delayed render'); 但当我从一个方法调用它时,这不是pass: class Test { constructor() { this.consoleLog('render'); // Test {} r
console.log('render');
console.log.delay(1000, 'delayed render');
但当我从一个方法调用它时,这不是pass:
class Test {
constructor() {
this.consoleLog('render'); // Test {} render
this.consoleLog.delay(1000, 'delayed render'); // undefined delayed render
}
consoleLog(text) {
console.log(this, text);
}
}
我如何才能干净地传递“this”?我认为问题不在于延迟原型,而是调用
consoleLog
方法下的console.log
的上下文
如果您这样定义它,您将看到所需的上下文:
类测试{
构造函数(){
this.consoleLog=this.consoleLog.bind(this);
本文件为consoleLog(“呈现”);
此.consoleLog.delay(1000,“延迟渲染”);
}
控制台日志(文本){
console.log(这个,文本);
}
}
我相信现在发生的事情是,delay
在consoleLog
的上下文中被调用,而且由于delay
最终调用的是任何的内容,因此(params)
没有上下文(不能通过对象访问,即consoleLog('delayed render')
而不是someObj.consoleLog('delayed render')
),因此未定义
要使用伪stacktrace绘制它,请执行以下操作:
Normal execution context:
const test = new Test()
test.consoleLog('abc')
-> consoleLog gets called in the context of `test`
-> console.log thus displays `Test` as the object `this` is referring to
Delayed execution context:
const test = new Test()
-> delay gets called with consoleLog as `this`
-> this(params) gets executed (consolLog(params))
-> since consoleLog was not called ON an object (see test.consoleLog above)
its context is undefined in strict mode, thus `console.log(this, text)` will
show `undefined "delayed render"`
我认为问题不在于延迟原型,而在于调用consoleLog
方法下的console.log
的上下文
如果您这样定义它,您将看到所需的上下文:
类测试{
构造函数(){
this.consoleLog=this.consoleLog.bind(this);
本文件为consoleLog(“呈现”);
此.consoleLog.delay(1000,“延迟渲染”);
}
控制台日志(文本){
console.log(这个,文本);
}
}
我相信现在发生的事情是,delay
在consoleLog
的上下文中被调用,而且由于delay
最终调用的是任何的内容,因此(params)
没有上下文(不能通过对象访问,即consoleLog('delayed render')
而不是someObj.consoleLog('delayed render')
),因此未定义
要使用伪stacktrace绘制它,请执行以下操作:
Normal execution context:
const test = new Test()
test.consoleLog('abc')
-> consoleLog gets called in the context of `test`
-> console.log thus displays `Test` as the object `this` is referring to
Delayed execution context:
const test = new Test()
-> delay gets called with consoleLog as `this`
-> this(params) gets executed (consolLog(params))
-> since consoleLog was not called ON an object (see test.consoleLog above)
its context is undefined in strict mode, thus `console.log(this, text)` will
show `undefined "delayed render"`
如果您谈论的是传递作用域,那么您可以通过在函数调用结束时添加.bind(this)
来实现,或者将函数更改为function impression.ty作为您的答案,如果我将.bind(this)
添加到返回延迟(this.bind(this),wait,….params);
它在此传递函数,而不是父对象。什么是函数印象?为什么不delay(()=>this.consoleLog('delayed render'),100)
?让它变得更简单、更难调试有什么意义?我不想在每个文件中都导入lodash。我想像它们一样使用该类。我想使用js。是的,如果我想避免这个问题,我可以只使用lodash,但在导入50个的大型项目中,使用prototype更容易,我从来没有使用过prototypese global。这是一个函数对函数的函数,所以我想让它成为函数的方法:)如果你说的是传递作用域,那么你可以通过在函数调用结束时添加.bind(this)
来完成,或者将函数更改为function impression.ty作为你的答案,如果我将.bind(this)
添加到返回延迟(this.bind(this),wait,…params);
它在此传递函数,而不是父对象。函数印象是什么?为什么不delay(()=>this.consoleLog('delayed render'),100)
?让它变得更简单、更难调试有什么意义?我不想在每个文件中都导入lodash。我想像它们一样使用该类。我想使用js。是的,如果我想避免这个问题,我可以只使用lodash,但在导入50个的大型项目中,使用prototype更容易,我从来没有使用过prototypese global。它是函数对函数的函数,所以我想将其作为函数的方法:)