繁体   English   中英

JavaScript:Function.prototype

[英]JavaScript: Function.prototype

下面是从“好零件”中复制的一些代码,并进行了一些修改。

程序.html

<html><body><pre><script src="program.js">
</script></pre></body></html>

程式

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

var Quo_2 = function (string) {
    this.status = string;
};

Quo_2.method(get_status, function () { return this.status; });

var myQuo_2 = new Quo_2("confused 2");

document.writeln(myQuo_2.get_status());

错误

ReferenceError: get_status is not defined

get_status应该是一个字符串。 您正在尝试传递变量,但是该函数接受字符串作为函数名称。

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

var Quo_2 = function (string) {
    this.status = string;
};
// added quotes around get_status
Quo_2.method('get_status', function () { return this.status; });

var myQuo_2 = new Quo_2("confused 2");

document.writeln(myQuo_2.get_status());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM