[英]JavaScript line with semicolon behaving strange
I am facing a strange issue with this code. 我遇到了与此代码一个奇怪的问题。 I can fix it easily but I would like to know why this is happening.
我可以轻松修复它,但我想知道为什么会这样。
If I comment out child
below I get undefined
error in IFFY. 如果我在下面注释掉
child
,则会在IFFY中得到undefined
错误。 The function is located in global scope and should work everywhere. 该功能位于全球范围内,应可在任何地方使用。 Not sure why?
不知道为什么吗?
parent=function(){
parent.prototype.method1= function() {}
parent.prototype.property = true;
}
child=function() {
parent.call(this);
child.prototype = new parent();
}
child; // This is important, if I remove this, I get an undefined error in IFFY ?
(function(){
var instance1 = new child();
console.log( instance1 ); // Empty Object
var instance2 = new child();
console.log( instance2 ); // Object is not empty
}());
(there are also other issues with this code which I asked about in an extra question) ( 此代码还有其他问题 ,我在一个额外的问题中提出了这个问题)
This is just automatic semicolon insertion messing with you. 这只是自动分号插入而引起的混乱。 When you omit the
child
you get: 省略
child
您会得到:
child=function() {
parent.call(this);
child.prototype = new parent();
}
(function(){
var instance1 = new child();
console.log( instance1 ); // Empty Object
var instance2 = new child();
console.log( instance2 ); // Object is not empty
}());
Which is parsed as: 解析为:
child=function() {
parent.call(this);
child.prototype = new parent();
}(function(){ // FUNCTION CALL
var instance1 = new child();
console.log( instance1 ); // Empty Object
var instance2 = new child();
console.log( instance2 ); // Object is not empty
}());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.