Chome和Firefox阻止修改派生类'咖啡脚本中的原型
Chome and Firefox prevents the modification of a derived class's prototype in coffeescript?
我最近一直在尝试学习CoffeeScript。当我试图理解CoffeeScript中的继承系统时,我陷入了一个有点进退两难的境地。我在Chrome 40、Internet Explorer 11和Firefox 36中尝试过这一点,但只有Internet Exporer 11的性能达到了我的预期。
CoffeeScript:中的此代码
class Muppet
constructor: (@age, @hobby) -> # Why is this function empty?
answerNanny: -> "Everything's cool!"
class SwedishChef extends Muppet
constructor: (age, hobby, @mood) ->
super(age, hobby)
cook: -> 'Mmmm soup!'
用JavaScript:生成此代码
var Muppet, SwedishChef,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype; return child;
};
Muppet = (function() {
function Muppet(age, hobby) {
this.age = age;
this.hobby = hobby;
}
Muppet.prototype.answerNanny = function() {
return "Everything's cool!";
};
return Muppet;
})();
SwedishChef = (function(_super) {
__extends(SwedishChef, _super);
function SwedishChef(age, hobby, mood) {
this.mood = mood;
SwedishChef.__super__.constructor.call(this, age, hobby);
}
SwedishChef.prototype.cook = function() {
return 'Mmmm soup!';
};
return SwedishChef;
})(Muppet)
以下是我在Chrome中获得的控制台输出:
muppet = new Muppet(3,4)
-> Muppet {age: 3, hobby: 4, answerNanny: function}
chef = new SwedishChef(3,4,5)
-> SwedishChef {mood: 5, age: 3, hobby: 4, constructor: function, cook: function…}
Muppet.prototype.food = "potato"
-> "potato"
muppet
-> Muppet {age: 3, hobby: 4, answerNanny: function, food: "potato"}
chef
-> SwedishChef {mood: 5, age: 3, hobby: 4, constructor: function, cook: function…}
SwedishChef.prototype.food = "fish"
-> "fish"
chef
-> SwedishChef {mood: 5, age: 3, hobby: 4, constructor: function, cook: function…}
现在看来,如果我删除__extends
函数中的最后三行,特别是:
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype; return child;
在所有浏览器中,我可以自由地独立修改Muppet
和SwedishChef
的原型。当这些行被放回去时,我无法以任何方式修改Chrome中派生类SwedishChef
的原型。这包括修改基类Muppet
的原型。修改不会传播到子类。在Firefox中,我不能修改基类或子类的原型。
从这三行来看,目的似乎是让子类的原型绑定到父类的原型,但这只在InternetExplorer11中很明显。
简单地说,我想知道我在这一切中的错误在哪里。我知道浏览器有时可能会有奇怪的实现怪癖,但我想排除我在这方面犯的任何错误。感谢所有花时间和精力阅读和回答我问题的人。
我认为您只是错误地读取了控制台输出,如中所示,属性就在那里,但它要么隐藏在省略号(...
)后面,要么在__proto__
属性上,您需要扩展它。
然而,我很好奇为什么您要在事后修改原型,而不是将它们包含在类定义中。CoffeeScript类的一个要点不是不需要修改它们的内部工作方式吗?
这是代码的编译版本,包括您在控制台中执行的部分。它在FireFox和Chrome中运行良好:
var Muppet, SwedishChef, chef, muppet,
extend = function(child, parent) {
for (var key in parent) {
if (hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
},
hasProp = {}.hasOwnProperty;
Muppet = (function() {
function Muppet(age1, hobby1) {
this.age = age1;
this.hobby = hobby1;
}
Muppet.prototype.answerNanny = function() {
return "Everything's cool!";
};
return Muppet;
})();
SwedishChef = (function(superClass) {
extend(SwedishChef, superClass);
function SwedishChef(age, hobby, mood) {
this.mood = mood;
SwedishChef.__super__.constructor.call(this, age, hobby);
}
SwedishChef.prototype.cook = function() {
return 'Mmmm soup!';
};
return SwedishChef;
})(Muppet);
muppet = new Muppet(3, 4);
chef = new SwedishChef(3, 4, 5);
Muppet.prototype.food = "potato";
console.dir(muppet);
SwedishChef.prototype.food = "fish";
console.dir(chef);
- 如果其他是咖啡脚本
- 咖啡脚本意外换行符
- 咖啡脚本意外的条件评估
- Wing IDE 中咖啡脚本的语法突出显示
- 如何在咖啡脚本中识别提交按钮,Rails 3应用程序
- 从函数(咖啡脚本)内以定时间隔触发函数
- 带有全局变量、咖啡脚本和回调的模块 CSV
- Chome和Firefox阻止修改派生类'咖啡脚本中的原型
- 为什么这个咖啡脚本没有编译“;正确”;
- 咖啡脚本中的回调
- 我的基于节点的咖啡脚本未运行(错误%1不是有效的应用程序.)
- 咖啡脚本和浏览器缓存
- 如何将一行咖啡脚本代码转换为多行
- 咖啡脚本数组查找下一个位置
- 咖啡脚本一行,用于创建带有变量键的哈希图
- 使用表单输入中的咖啡脚本编辑保存的值
- 咖啡脚本中的嵌套方法
- 咖啡脚本化的 ajax 调用结果在实际成功时失败回调
- 在同一项目上使用咖啡脚本和打字稿
- 咖啡脚本、类和函数