Javascript 原型中的调用函数 功能测试() { console.log('constructor'); this.chaninFunction=函数(){ console.log('chain me up'); } } Test.prototype.callme=函数(第一个参数){ console.log('called him'); this.callBack=函数() { console.log('callback'); } }; Test.prototype.message=函数(第一个参数){ console.log('message him'); }; var测试=新测试(); test.chaninFunction(); test.callme(); test.callme().callBack()//错误未定义 test.message();
嗨 我现在正在学习JS。经历很少的情况Javascript 原型中的调用函数 功能测试() { console.log('constructor'); this.chaninFunction=函数(){ console.log('chain me up'); } } Test.prototype.callme=函数(第一个参数){ console.log('called him'); this.callBack=函数() { console.log('callback'); } }; Test.prototype.message=函数(第一个参数){ console.log('message him'); }; var测试=新测试(); test.chaninFunction(); test.callme(); test.callme().callBack()//错误未定义 test.message();,javascript,Javascript,嗨 我现在正在学习JS。经历很少的情况 有没有办法在原型中调用函数?我所做的上述测试导致了错误。我如何访问prototype中的函数,或者我无法访问?您似乎在说您希望在调用.callme()后能够链接.callback() 链接非常简单。您调用的前一个方法只需要返回一个包含您要调用的下一个方法的对象。所以在您的例子中,两个方法都在同一个对象上,所以您只需要执行返回这个 <script type="text/javascript"> function Test() { con
有没有办法在原型中调用函数?我所做的上述测试导致了错误。我如何访问prototype中的函数,或者我无法访问?您似乎在说您希望在调用
.callme()
后能够链接.callback()
链接非常简单。您调用的前一个方法只需要返回一个包含您要调用的下一个方法的对象。所以在您的例子中,两个方法都在同一个对象上,所以您只需要执行返回这个代码>
<script type="text/javascript">
function Test()
{
console.log('constructor');
this.chaninFunction = function(){
console.log('chain me up');
}
}
Test.prototype.callme = function(first_argument) {
console.log('called him');
this.callBack = function()
{
console.log('call back');
}
};
Test.prototype.message = function(first_argument) {
console.log('message him');
};
var test = new Test();
test.chaninFunction();
test.callme();
test.callme().callBack(); //Error undefined
test.message();
</script>
您正在将callBack
附加到实例本身,因此在调用test.callme()之后
,您只需执行test.callback()代码>。但是,我不知道为什么这会有用。@FelixKling谢谢你的帮助:)test.callback()结果错误Uncaught TypeError:Object#没有方法“callback”@user775735:没有:正如我所说,只有在调用test.callme()后,函数才可用。
callme
应该返回这个
如果您希望能够执行test.callme().callback()
。
Test.prototype.callme = function(first_argument) {
console.log('called him');
this.callBack = function()
{
console.log('call back');
}
return this;
};