var eatFunction = function (what_to_eat) { var sentence = ‘I am going to eat a ‘ + what_to_eat; console.log( sentence ); }; eatFunction( ‘sandwich‘ ); // is the same as (function (what_to_eat) { var sentence = ‘I am going to eat a ‘ + what_to_eat; console.log(sentence); })(‘sandwich‘); // is the same as (function (what_to_eat) { var sentence = ‘I am going to eat a ‘ + what_to_eat; console.log(sentence); }(‘sandwich‘)); // Three of them output ‘I am going to eat a sandwich‘
唯一的区别是,变量 eatFunction 被移除了,使用一对括号把函数定义包了起来。
时间: 2024-10-12 03:50:06