javascript - Using apply() in IIFE -
i'm trying use apply() within iife. error 'intermediate value not function' going wrong ?
var person = { getfullname : function(firstname, lastname) { return firstname + ' ' + lastname; } } /* iife - using apply */ (function(firstname, lastname) { getname = function(){ console.log("from iife.."); console.log(this.getfullname(firstname, lastname)); } getname(); }).apply(person, ['john', 'doe']);
plnnkr : http://plnkr.co/edit/77db8mu4i9rxgqt26pap?p=preview
the problem ;
, in code person
object syntax not terminated, , iife considered continuation of that.
read: automatic semicolon insertion
if @ external libraries, iife statement starts ;
, used escape scenario.
also note that, inside getname
function this
not refer person
object, need either use closure variable or pass value this
manually.
var person = { getfullname: function(firstname, lastname) { return firstname + ' ' + lastname; } } /* iife - using apply */ ; (function(firstname, lastname) { var self = this; var getname = function() { console.log("from iife.."); console.log(self.getfullname(firstname, lastname)); } getname(); }).apply(person, ['john', 'doe']);
Comments
Post a Comment