Originally posted by barrydidit
View Post




function myFunction() {
console.log('in the function');
this.something = 'Hello';
}
myFunction();
// Call the function
// outputs 'in the function'
var myObj = new myFunction();
// Creates an object using the function as constructor
console.log(myObj.something);
// outputs 'Hello'
var appVar1 = {};
(
function(appVar2){
appVar2.MyConstructor = function() {
//...
};
}
) (appVar1);
var appVar1 = {};
(
function(appVar2){
appVar2.MyConstructor = function() {
//...
};
}
) (appVar1);
var appVar1 = {};
function fn(appVar2){
appVar2.x = 10;
}
fn(appVar1)
var appVar1 = {};
function fn(appVar2){
appVar2.x = 10;
}
fn(appVar1)
var appVar1 = {};
function fn(appVar2){
appVar2.x = 10;
}
fn(appVar1)
var appVar1 = {}; //declare object literal (equiv. to "var appVar = new Object();")
//wrap an anonymous function in parens to make it an expression, and then invoke immediately on the final line, passing in appVar1 (visible inside the anonymous function as appVar2)
(function(appVar2){
//create a property named MyConstructor on appVar2 (which is a reference to the same object as appVar1), and assign an anonymous function to it
appVar2.MyConstructor = function() {
//...
};
})(appVar1);
//object "appVar1" now has a new property on it called MyConstructor pointing to the function defined above with the ellipsis inside.
var appVar1 = {};
(function(appVar2){
appVar2.MyConstructor = function() {
//...
};
})(appVar1);


Leave a comment: