global objects and passing by reference - javascript

I have a global variable 'globalProperty' and assign it to a property of an object'.
var globalProperty = 'hello';
function func1(){
obj['prop'] = globalProperty;
//code....
obj['prop'] = 'good'.
}
I want my obj[prop] to reference the global variable. SO if I modify the obj[prop], the global variable will also be modified to 'good'.

javascript always use by value for passing params, you can still create a global object and modify its properties , for example
var globalObject= new Object();
globalObject.text = 'Hello';
function func1() {
obj = {};
obj['prop'] = globalObject;
console.log(globalObject.text);
obj['prop'].text = 'good';
console.log(globalObject.text);
}
func1();

Related

javascript: How to destory class Object and make calling variable undefined?

How do I properly destroy a class object of MyClass and make the calling variable myObj = undefined?
var namespace = {};
namespace.instance = {};
function MyClass(name) {
this.name = name;
this.destroy = function() {
delete namespace.instance[name];
}
namespace.instance[name] = this;
}
var myObj = new MyClass('test');
console.log(myObj);
myObj.destroy();
console.log(myObj) // make this undefined;
In JavaScript, you cannot make a variable point to undefined without explicitly assigning undefined to the variable name. After you do
var myObj = new MyClass('test');
the only way to make myObj undefined is to do
myObj = undefined;
afterwards.
Given only a reference to the object (such as in a destroy method), there's no way to break the outer myObj reference to the object; all you can do in destroy is mutate the object, but it'll still remain an object. What you're looking for isn't really possible in JS.

Difference between 'this' and normal variable declaration

What is the difference between declaring a variable with this in my function than declaring normally using let or var?
const controller = (function() {
this.name = 'John';
})();
const controller2 = (function() {
let name = 'Mary';
})();
this inside your function is window object. So you are not creating a variable, you are adding a property to the window object
console.log((function(){return this})() === window)

Accessing variables from IIFE

I have the following closure:
var Container = (function () {
var variable;
var changeVariable = function () {
variable = 5;
};
return {
variable: variable,
changeVariable: changeVariable
};
})();
Container.changeVariable();
console.log(Container.variable);
The result is undefined, unless I set variable as:
Container.variable = 5
Why is that so? What's the difference? How should I do this properly?
Why is that so?
JavaScript assigns by value.
variable = 5; assigns the value 5 to the variable variable.
variable: variable, assigns the value of variable (at the time the code runs) to the property variable. It does not create a reference to the variable called variable.
When you later change the value of the variable called variable, you don't change the value of the property called variable.
How should I do this properly?
Create an object. Store the object locally. Manipulate that object. Return that object.
Forget about having the variable called variable entirely.
var container = (function() {
var self = {
variable: undefined,
changeVariable: changeVariable
};
function changeVariable() {
self.variable = 5;
}
return self;
})();
container.changeVariable();
console.log(container.variable);
(Aside: Convention reserves identifiers beginning with capital letters for constructor functions. I've renamed Container to follow that convention).
Use a getter:
return {
get variable() { return variable; },
changeVariable: changeVariable
};

Accessing object properties with prototype

I know there are several ways to create Objects, add Methods and Properties etc.
There is one thing I don't understand about prototypes
function Obj () {
var msg = "message";
this.log = function (){
console.log(msg);
}
}
var o = new Obj();
o.log();
this would output message. however using a prototype
function Obj2 () {
var msg = "message2";
}
Obj2.prototype.log = function () {
console.log(msg);
}
var o2 = new Obj2();
o2.log();
would output that msg is not defined.
why is that?
msg is a variable that is scoped to Obj and Obj2 respectively.
The log function in the first example is defined within the scope of Obj so it has access to variables from that scope.
The log function in the second example is not defined within that scope of Obj2, so it doesn't.
You have a scope problem as mentioned by #Quentin, However if you need to access the variable inside the prototype function, you need to make use of this and treat it as a class. So now it will be treated as a property to the class Obj2 and not as a private variable.
So the code will be like:
function Obj2 () {
this.msg = "message2";
}
Obj2.prototype.log = function () {
console.log(this.msg);
}
var o2 = new Obj2();
o2.log();
Fiddle Demonstration

javascript: passing global var to function

I have a global variable MyGlobalVar and some code that looks like this:
var MyGlobalVar = null;
function PlayWithMyGlobal() {
MyGlobalVar = new Object();
.... adding properties to MyGlobalVar
MoreFun(MyGlobal);
}
function MoreFun(TheVar) {
is TheVar here a local or just a reference to the global?
}
If I pass the global variable, am I still working with the global?
Thanks.
If I pass the global variable, am I still working with the global?
Thanks.
It depends whether variable you pass is an object or a primitive (number, string, boolean, undefined, null are primitives) value in the first place. Objects are passed by reference and primitives by value.
In your case, you are passing object:
MyGlobalVar = new Object();
And in JS, objects are passed by reference. So you are still working on that variable.
You can confirm it like this:
var MyGlobalVar = null;
function PlayWithMyGlobal() {
MyGlobalVar = new Object();
MoreFun(MyGlobalVar);
}
function MoreFun(TheVar) {
MyGlobalVar.foo = 'I am foo'; // property created here
}
PlayWithMyGlobal();
console.log(MyGlobalVar.foo); // I am foo
If the global variable is an object, then you're still working with the global variable. Otherwise, it's a copy.
As shown and annotated below, your variables point to the same global object.
var MyGlobalVar = null;
function PlayWithMyGlobal() {
MyGlobalVar = new Object(); // <--- Object
MoreFun(MyGlobalVar); // <--- Passing object reference
}
function MoreFun(TheVar) {
TheVar.test = 'Did I modify global?';
alert(TheVar === MyGlobalVar); // true
alert(MyGlobalVar.test); // "Did I modify global?"
}
Yes, you have a local reference to the same object that is referenced globally.
A simple test would be...
console.log(MyGlobalVar === TheVar); // should be true

Categories

Resources