JavaScript函数写法整理

mac2022-06-30  126

1.普通函数定义的两种写法

function hello(){ console.log("hello!"); } var hello = function(){ console.log("hello!"); }

2.函数自执行,快速初始化变量(两种写法hello变量值为15)

#函数自执行 var hello = function(num){ return 3 * num; }(5); #普通写法 var hello = 5; hello = 3 * hello;

3.函数预执行

(function(){ console.log('Hello '); })();

4.闭包

function say(name){ var info = 'Hello ' + name; return function(){ console.log(info); } } var s= say('bret'); s();

  

转载于:https://www.cnblogs.com/bretgui/p/9970022.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)