This question already has answers here:
When is the body of a Promise executed?
(4 answers)
Javascript: Access Promise object from within function passed to Promise constructor
(2 answers)
Closed 21 days ago.
I'm just experimenting and came across this:
var d = 7;
var p1 = new Promise((a, b) => {
console.log(p1);
console.log(d);
a('whatever');
});
I'm just curious about what's going on here. The fact that I'm really sure this gonna work, but doesnt, means there's a gap in my understanding. could anyone explain why p1 is not reachable from inside a, but d is?
workaround is easy, but I just want to understand this one.
Thanks.
Related
This question already has answers here:
How is almost everything in Javascript an object?
(6 answers)
Closed 5 months ago.
I tested this today and I don't understand the behaviour. For me, that should be an error.
const a = 1;
a.test = 1;
console.log(a)
console.log(a.test)
Someone have the answer ?
you can define properties pretty much anything, but in this case you are assigning it to a number which doesn't actually hold properties.
This question already has answers here:
javascript if giving "undefined" in console [duplicate]
(2 answers)
Closed 3 years ago.
I am following a book on Javascript and the example doesn't seem to return any value. I don't have much experience with OOP though.
I believe this is a programmatical way to create classes, but it returns undefined any way
What do you make of this?
Thank you in advance
This is expected, variable declarations always return undefined.
E.g., let a = 0, var b = 0, const c = 0.
This doesn't mean a, b, and c are undefined though.
Likewise for your example, createClass and Book aren't undefined, as seen by your console.log.
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 6 years ago.
I am trying to lear generators but before going into that I want to know how/why this works
function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
return nextIndex++
}
}
}
var it = makeIterator([1,2,3,4]);
Why is it that it.next() return 1 and another it.next() returns 2. How does it know?
Is there a way to see how it works behind the scenes?
An iterator is an object with state. In your case, the state is the private nextIndex closure variable, that's how it keeps track.
There's no magic going on behind the scenes here, if you didn't have that variable, you'd get an error.
This question already has answers here:
Method vs Functions, and other questions
(8 answers)
What's the difference between a method and a function?
(41 answers)
Closed 7 years ago.
I know it's a very basic question, but since I am a beginner I have very limited knowledge about it. I tried to understand it by googling various online sources, but could not get a crystal clear view of it. Thanks in advance.
A method, in JavaScript, is a function that is set on an object property; no more, no less.
window.f = function() {} // method of `window`
a = {
g: function() {} // method of `a`
};
function x() {
var h = function() {} // not a method, because it's in a local variable,
// not in an object attribute
var b = { i: h }; // method of `b` now.
};
This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 7 years ago.
So, I recently had a pretty weird problem when developing a library, safe_children.
When I was writing the examples, I decided to try making it like this:
var child = new Child('otherFile.js', 3 * 60);
child.loadScript()
.then(child.spawn);
This code doesn't work. this points to something I couldn't find out. However, this piece of code works:
var child = new Child('otherFile.js', 3 * 60);
child.loadScript()
.then(function(){
child.spawn();
});
Anyone knows why? What is this in this context? Thanks!
Your issue here has nothing to do with promises.
You are passing in child.spawn, which is nothing more than a function. Your promise has no way to know that it belongs to child, so all it can do is call it. Therefore, this will most likely either be null, undefined, or the window object.
You can see the same behavior by doing:
var sp = child.spawn;
sp(); // <---- `this` is not `child` here
You can get around this by doing:
.then(child.spawn.bind(child));
or by doing what you've already done.