Giving some quick examples
function Droppable() {
this.relevant = true;
this.uuid = generateUUID();
}
// this will throw an error because you are calling generateUUID before it initialize
console.log(new Droppable);
var generateUUID = function() {
return '12345';
}
// this will give you actuall output what you wanted
console.log(new Droppable);
So when you will call function generateUUID before calling Droppable it will work. Like this -
// initializes
var generateUUID = function() {
return '12345';
}
// initializes
function Droppable() {
this.relevant = true;
this.uuid = generateUUID();
}
// this will work as expected
console.log(new Droppable);
In JavaScript you can define function in two method
var varibaleName = function(){
}
and
function variableName(){
}
There is a significant differences. When you are declaring a function with a variable then before call that function that variable need to register.
if you call like this -
varibaleName();
var varibaleName = function(){
}
this will give you an error.
But if you call like this it will work -
varibaleName();
function varibaleName (){
}
So this should work too -
function Droppable() {
this.relevant = true;
this.uuid = generateUUID();
}
// this will work as expected
console.log(new Droppable);
function generateUUID() {
return '12345';
}
Hope this make sense.
However in angular this will work -
var myApp = angular.module("myApp", []);
myApp.controller("MyCtrl", 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];
console.log( $scope.region1 );
});
But best practice is -
myApp.controller("MyCtrl", function($scope){
// you can call this before Droppable or after
$scope.generateUUID = function() {
return '12345';
}
$scope.Droppable = function () {
this.relevant = true;
this.uuid = $scope.generateUUID();
}
// initalize droppable areas
$scope.region1 = [new $scope.Droppable];
console.log( $scope.region1 );
});
Why i am calling those with $scope.
prefix, because later you may need to talk between controller to controllers even in directives!
Happy coding and happy prototyping! How this helps you to understand how things are working.