Variable undefined when updated inside a function [duplicate] - javascript

This question already has answers here:
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I declare user_properties outside of my function. I then get my data from firebase and try to store it in user_properties but it ends up being undefined.
Does anyone know what I am doing wrong here?
var user_properties;
get_user_properties = new Firebase("https://<MY-URL>/users/"+auth.uid+"/properties");
get_user_properties.once('value', function (dataSnapshot) {
user_properties = dataSnapshot.val();
});
//undefined
console.log(user_properties);

I've provided for you a simple example.
var foo,
obj = new Bar("some text");
obj.set("cute cat", function(cat){
foo = "my lovely cat";
});
console.log(foo); // undefined
obj.doo();
console.log(foo); // "my lovely cat"
With this constructor function
function Bar(c){
this.mcat = c;
this.callback;
/* this method only saves that callback function */
this.set = function(catname, callback){
this.mcat = catname;
this.callback = callback;
}
/* this method calls that callback function */
this.doo = function(){
this.callback();
}
}

Related

this is window inside the function of a class [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 1 year ago.
I have some code here.
I am aware that this reference wont be carried in anonymous functions.
But here, even when using a function of an object, the this inside that is window.
var MyClass = function (div) {
this.array = [];
};
MyClass.prototype = {
addToArray: function (elem) {
this.array.push(elem);
},
processAndAdd: function(elemArray){
elemArray.forEach(this.addToArray);
}
}
var myObj = new MyClass();
myObj.processAndAdd([1, 2, 3]);
console.log('test');
Error: Line: this.array.push(elem);
push of undefined.
On inspection, the this here is window Object
I want to know why this is window here, and how to restructure my code to properly handle this.
The reference to this object is no more MyClass constructor after you pass the callback function body as callback to forEach. Bind the function body with the current this context and your code should work.
var MyClass = function (div) {
this.array = [];
};
MyClass.prototype = {
addToArray: function (elem) {
this.array.push(elem);
},
processAndAdd: function(elemArray){
elemArray.forEach(this.addToArray.bind(this));
}
}
var myObj = new MyClass();
myObj.processAndAdd([1, 2, 3]);
console.log('test');

What is the difference between a constructor and a function in javascript? [duplicate]

This question already has answers here:
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 4 years ago.
What is the purpose of constructor and new operator in javascript and why it is used on functions as we already have function calls in which returned value is reliable. what is the purpose of this constructor as it gives the value same as the function call?
If you wish to emulate a class functionality in your application you can use functions as impromptu classes to store and handle objects.
You can also scope these classes to your local function
function Foo() {
this.bar = new Date();
}
Foo.prototype.getDate = function() {
return this.bar;
}
var foo = new Foo();
console.log(foo.getDate());
+function() {
var Foo = function() {
this.bar = new Date();
}
Foo.prototype.getDate = function() {
return this.bar;
}
/**
* scoped Foo works
*/
var foo = new Foo();
console.log(foo.getDate());
}();
/**
* Outside of function scope, Foo is unkown
*/
var foo = new Foo();
console.log(foo.getDate());

Angular 2 - Using 'this' inside setTimeout [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
I have a function like so in my class
showMessageSuccess(){
var that = this;
this.messageSuccess = true;
setTimeout(function(){
that.messageSuccess = false;
},3000);
}
How can I re-write this so I don't have to store a reference to 'this' in the 'that' var? If I use 'this' inside the setTimeout, the messageSuccess bool doesn't seem to change/get updated.
You need to use Arrow function ()=> ES6 feature to preserve this context within setTimeout.
// var that = this; // no need of this line
this.messageSuccess = true;
setTimeout(()=>{ // <<<---using ()=> syntax
this.messageSuccess = false;
}, 3000);

Can we call the function by function name after assigning it to a variable.? [duplicate]

This question already has answers here:
Javascript functions like "var foo = function bar() ..."?
(9 answers)
Closed 6 years ago.
Have a function assigned to a variable
var sq = function a(s){return s * s};
Now if i call sq(4) i am getting the result as 16 but if i try to call using the function name a(4) it throws an error "a is not defined"
Can we not call the function by its name once it is assigned to a variable.?
No, because you're using an assignment. You can just do var fn = function () { } or var fn = ()=> { } and get the same thing.
If you created the function and then assigned it to a variable, you could do both.
Basically,
function fn() {...}
var myFunc = fn;
You could then do both:
myFunc();
fn();

this inside callback function [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 8 years ago.
Here is my situation.
function Bird() {
this._canFly = true;
this._legs = 2;
this._flying = false;
}
Bird.prototype = {
Fly: function() {
if ( this.canFly ) {
layer.on('fly', function() {
this.setStrokeWidth(4); //this refers to layer(kinetic.js) object
this._flying = true; //this refers to Bird object
});
}//end if
} //end function
);
Here I need to access both layer object and bird object inside the callback function.
Can somebody tell me how to handle the above situation ?
var self = this
Cache a reference to this to refer to it when it changes context.

Categories

Resources