在javascript中调用函数

时间:2012-06-20 13:12:10

标签: javascript

  

可能重复:
  What do parentheses surrounding a JavaScript object/function/class declaration mean?

为什么以下代码不起作用

function func() {
    document.writeln("HELLO");
}();

更新:
为什么以下示例O不应该在“function {}”周围使用parentesis?

var v = function() {
   return "HELLO";
}();

document.writeln(V);

2 个答案:

答案 0 :(得分:4)

使用此:

function func() {
    document.writeln("HELLO");
}

func();

或者这个:

(function() {
    document.writeln("HELLO");
})();

答案 1 :(得分:1)

尝试:

(function() {
  document.writeln('HELLO');
})();