Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

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 原型中的调用函数 功能测试() { 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

我现在正在学习JS。经历很少的情况


有没有办法在原型中调用函数?我所做的上述测试导致了错误。我如何访问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;
};