首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解原型设计和增强

理解原型设计和增强
EN

Stack Overflow用户
提问于 2011-10-19 02:44:11
回答 1查看 2.1K关注 0票数 0

我有一个fiddle可以帮助我理解JavaScript原型和继承。这些评论说明了这个故事。

代码语言:javascript
复制
//From Douglas Crockford's "Javascript: the good parts":  a helper to hide the "ugliness" of setting up a prototype
Function.prototype.method = function(name,func) {
    this.prototype[name] = func;
    return this;
}

function SomeFunc(value) {
    this.setValue(value);
}

//Inherit the function (to me this conceptually the same as adding a member to a class)
SomeFunc.method('setValue', function (value) {
    this.value = value;
    return this;
});

try
{
SomeFunc(1);
}
catch(e)
{
    alert(e);
}

为什么我会得到一个异常?我的注释是否正确,因为JavaScript调用继承就是给经典程序员简单地向类添加一个新成员?增强和继承之间的区别是什么?

EN

回答 1

Stack Overflow用户

发布于 2011-10-19 03:11:41

相反,尝试:

代码语言:javascript
复制
new SomeFunc(1);

因为这是原型工作时的情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7812085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档