javascript - Angular function constructor in Controller -
in normal js can this:
function droppable() { this.relevant = true; this.uuid = generateuuid(); }; var generateuuid = function() { return '12345'; } console.log(new droppable); // returns droppable {relevant: true, uuid: "12345"} but in angular have this:
angular.module('myapp').controller('myctrl', ['$scope', function($scope) { function droppable() { this.relevant = true; this.uuid = generateuuid(); } var generateuuid = function() { return '12345'; } // initalize droppable areas $scope.region1 = [new droppable]; $scope.region2 = [new droppable]; $scope.region3 = [new droppable]; }]); i trying make 3 droppable areas uuid.
but when 'undefined not function' referring line this.uuid = generateuuid(); in function droppable() {...}
why that?
turning comment answer. issue due variable holding function expression being used before has been assigned, in case variable generateuuid not defined yet.
var generateuuid = function() { return '12345'; } one way fix it, turn function declaration automatically hoisted top of scope.
function generateuuid () { return '12345'; } or define variable generateuuid function reference before possible usage.
//some variable declarations, start of scope var generateuuid = function() { return '12345'; } function droppable() { this.relevant = true; this.uuid = generateuuid(); } when do:-
.... code .... var generateuuid = function() { return '12345'; } .... code ... it same as:-
var generateuuid; .... code .... //any usage of till here cause error facing, since there no value assigned generateuuid yet generateuuid = function() { return '12345'; } .... code ... and plnkr
note:- issue should not happen in example because invoking constructor after (generateuuid) has been assigned value, different usecase (which not present in question) might having issue.
Comments
Post a Comment