Why is this javascript object with simplest constructor undefined? -
i tested in console :
var toto = (function() {function toto() {}})(); toto result in chrome console
undefined why ? have expected usual constructor :
function toto() {} what syntax error did make ?
if run code believe give answer.
var noname = (function() { function toto() { console.log('running toto'); return 'returning toto'; } console.log(toto()); return 'no name'; })(); console.log(noname); the longer answer following.
(function(){})(); this called iife (immediately invoked function expression) creates , calls function right after created. place in it, contained function. best way create private variables in es5 javascript. functions can return things iife , access variables declared outside iife nothing within outside, again creates private scope. have iife return nothing returns default value undefined.
Comments
Post a Comment