admin管理员组文章数量:1307530
I ran into what seems to be a silly issue with some Javascript:
go = function () {
alert("Go!");
}
$(function () {
go();
});
When the page loads I get an error:
Webpage error details
User Agent: Mozilla/4.0 (patible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; .NAP 1.1) Timestamp: Thu, 17 Mar 2011 20:18:03 UTC
Message: Object doesn't support this property or method Line: 1 Char: 1 Code: 0 URI: http://localhost:61710/Scripts/number.js
When I change the go
initializer to this:
function go() {
alert("Go!");
}
...everything works just fine.
What am I missing? Also, is there a reason to use one form of function initializer over the other?
Edit: I get this error when I run the code in an instance of IE8 using the built-in Visual Studio web server (Start without Debugging). When I run the code in a separate instance of IE8 without Visual Studio, it works just fine. Perhaps Visual Studio forces IE to use stricter JS piler settings?
I ran into what seems to be a silly issue with some Javascript:
go = function () {
alert("Go!");
}
$(function () {
go();
});
When the page loads I get an error:
Webpage error details
User Agent: Mozilla/4.0 (patible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; .NAP 1.1) Timestamp: Thu, 17 Mar 2011 20:18:03 UTC
Message: Object doesn't support this property or method Line: 1 Char: 1 Code: 0 URI: http://localhost:61710/Scripts/number.js
When I change the go
initializer to this:
function go() {
alert("Go!");
}
...everything works just fine.
What am I missing? Also, is there a reason to use one form of function initializer over the other?
Edit: I get this error when I run the code in an instance of IE8 using the built-in Visual Studio web server (Start without Debugging). When I run the code in a separate instance of IE8 without Visual Studio, it works just fine. Perhaps Visual Studio forces IE to use stricter JS piler settings?
Share Improve this question edited Dec 21, 2011 at 11:29 Raidok 7648 silver badges19 bronze badges asked Mar 17, 2011 at 20:24 Chad LevyChad Levy 10.1k8 gold badges43 silver badges69 bronze badges 4- can you post what browser you are using? – Naftali Commented Mar 17, 2011 at 20:26
- Your original works for me: jsfiddle/Xz3s5 on IE8, FF3.3.6, Chrome 10.0.648.151, and Opera 10.63. – tvanfosson Commented Mar 17, 2011 at 20:36
- I'm using IE8. See the user agent section of the error details. – Chad Levy Commented Mar 17, 2011 at 20:38
- Looks like I only get the error when the IE8 instance is attached to Visual Studio. – Chad Levy Commented Mar 17, 2011 at 20:58
3 Answers
Reset to default 8You should declare the variable first:
var go = function () {
alert("Go!");
}
One reason to use this form is that it can help and avoid polluting the global namespace with your functions (see an example of this notion here).
The difference (and may help you decide which is better over another) is that
go = function () {
alert("Go!");
}
is defined at parse-time whereas
function go() {
alert("Go!");
}
is defined at run-time.
P.S., it works for me, however you may need to do:
var go = ...
rather than
go = ...
work fine for me:
http://jsfiddle/vEKgX/
although try this instead:
var go = function () {
alert("Go!");
}
$(go);
本文标签: javascriptfunction go() vs gofunction()Stack Overflow
版权声明:本文标题:javascript - `function go()` vs `go = function()` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741842862a2400607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论