firefox上,这个是可以正常运行的:<html>
<head>
<SCRIPT> (function(){
Album.prototype =Bert.prototype;
})();function Album () {}function Bert () {}
Bert.prototype = {
abc: function() {
alert("Bert.abc");
}
}var c = new Bert;
c.abc();</SCRIPT>
</head>
<body >
</body>
</html>而换个位置,就firebug就会报错: ({abc:(function () {alert("Bert.abc");})}) is not a function
为什么呢?以下:错误的代码<html>
<head>
<SCRIPT> function Album () {}function Bert () {}
Bert.prototype = {
abc: function() {
alert("Bert.abc");
}
}(function(){
Album.prototype =Bert.prototype;
})();var c = new Bert;
c.abc();</SCRIPT>
</head>
<body >
</body>
</html>
<head>
<SCRIPT> (function(){
Album.prototype =Bert.prototype;
})();function Album () {}function Bert () {}
Bert.prototype = {
abc: function() {
alert("Bert.abc");
}
}var c = new Bert;
c.abc();</SCRIPT>
</head>
<body >
</body>
</html>而换个位置,就firebug就会报错: ({abc:(function () {alert("Bert.abc");})}) is not a function
为什么呢?以下:错误的代码<html>
<head>
<SCRIPT> function Album () {}function Bert () {}
Bert.prototype = {
abc: function() {
alert("Bert.abc");
}
}(function(){
Album.prototype =Bert.prototype;
})();var c = new Bert;
c.abc();</SCRIPT>
</head>
<body >
</body>
</html>
匿名函数前的部分得用分号隔开
<html>
<head>
<SCRIPT> function Album () {}function Bert () {}
Bert.prototype = {
abc: function() {
alert("Bert.abc");
}
};(function(){
Album.prototype =Bert.prototype;
})();var c = new Bert;
c.abc();</SCRIPT>
</head>
<body >
</body>
</html>