javascript - var functionName = function() {} vs functionName() {}

Mots clés : javascriptfunctionsyntaxidiomsjavascript

meilleur 4 Réponses javascript - var functionName = function() {} vs functionName() {}

vote vote

91

// TypeError: functionOne is not a function  functionOne();    var functionOne = function() {    console.log("Hello!");  };
// Outputs: "Hello!"  functionTwo();    function functionTwo() {    console.log("Hello!");  }
'use strict';      { // note this block!    function functionThree() {      console.log("Hello!");    }  }  functionThree(); // ReferenceError
vote vote

80

function xyz(){   function abc(){};   // abc is defined here... } // ...but not here 
var xyz = function abc(){}; 
var xyz = function abc(){   // xyz is visible here   // abc is visible here } // xyz is visible here // abc is undefined here 
function abc(){}; var xyz = abc; 
console.log(xyz === abc); // prints "true" 
function abc(){}; console.log(abc.name); // prints "abc" 
var abc = function(){}; console.log(abc.name); // prints "" 
// Assume really.long.external.scoped is {} really.long.external.scoped.name = function shortcut(n){   // Let it call itself recursively:   shortcut(n - 1);   // ...   // Let it pass itself as a callback:   someFunction(shortcut);   // ... } 
function abc(){} 
// We can call it here abc(); // Works  // Yet, it is defined down there. function abc(){}  // We can call it again abc(); // Works 
// We can call it here abc(); // Works return; function abc(){} 
var xyz = function(){}; 
// We can't call it here xyz(); // UNDEFINED!!!  // Now it is defined xyz = function(){}  // We can call it here xyz(); // works 
var xyz = function abc(){}; console.log(xyz.name); // Prints "abc" 
var abc = function(){}; 
abc = function(){}; 
function abc(){}; 
vote vote

74

function x() {     console.log('x'); } 
x(); // Works even though it's above the declaration function x() {     console.log('x'); } 
if (someCondition) {     function foo() {    // <===== HERE THERE     }                   // <===== BE DRAGONS } 
"use strict"; if (someCondition) {     foo();               // Works just fine     function foo() {     } } console.log(typeof foo); // "undefined" (`foo` is not in scope here                          // because it's not in the same block) 
var y = function () {     console.log('y'); }; 
var z = function w() {     console.log('zw') }; 
var z = function w() {     console.log(typeof w); // "function" }; console.log(typeof w);     // "undefined" 
var obj = {     value: 0,     get f() {         return this.value;     },     set f(v) {         this.value = v;     } }; console.log(obj.f);         // 0 console.log(typeof obj.f);  // "number" 
var a = [1, 2, 3]; var b = a.map(n => n * 2); console.log(b.join(", ")); // 2, 4, 6 
var a = [1, 2, 3]; var b = a.map((n, i) => n * i); console.log(b.join(", ")); // 0, 2, 6 
var a = [   {first: "Joe", last: "Bloggs"},   {first: "Albert", last: "Bloggs"},   {first: "Mary", last: "Albright"} ]; a = a.sort((a, b) => {   var rv = a.last.localeCompare(b.last);   if (rv === 0) {     rv = a.first.localeCompare(b.first);   }   return rv; }); console.log(JSON.stringify(a)); 
var o = {     foo() {     } }; 
var o = {     foo: function foo() {     } }; 
class Person {     constructor(firstName, lastName) {         this.firstName = firstName;         this.lastName = lastName;     }      getFullName() {         return this.firstName + " " + this.lastName;     } } 
vote vote

60

 alert(typeof foo); // 'function', it's already available  alert(typeof bar); // 'undefined'  function foo () {}  var bar = function () {};  alert(typeof bar); // 'function' 
 function test () {}  test = null; 

Questions similaires