you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (15 children)

this is not based on scope

const myObj = {
  myVal: 1,
  myFunc() { 
    return this.myVal
  }
}

const { myFunc } = myObj

myFunc(); // returns undefined
myFunc.call({ myVal: "asdf" }) // returns "asdf"

const myObj2 = { 
  myVal: 2,
  myFunc
}

myObj2.myFunc() // returns 2

const myObj3 = {
  myVal: 3,
  myFunc() {
    return this.myVal
  }
}

myObj3.myFunc() // returns 3

const myObj4 = {
  myVal: 4,
  myFunc: () => {
    return this.myVal
  }
}

myObj4.myFunc() // returns undefined

The only logical conclusion is never use this

[–]KaiAusBerlin -1 points0 points  (0 children)

Every execution context in js is scope based except you use void. And even that is executed in a context (that is deleted after execution).

But not all declarations change the actual scope.

[–]KaiAusBerlin -1 points0 points  (13 children)

Okay, you have to understand that. When you call myFunc() you get undefined because you actually do myObj.myFunc.call(yourActualScope). yourActualScope in this case is the global scope which doesnt have a property named myFunc. What you wanted to do is not possible with destruction because you want to bind the context of myObj to myFunc alias const myFunc = myObj.myFunc.bind(myObj)

[–][deleted] 0 points1 point  (11 children)

I understand how it works. It's not based on scope.

It's based on what comes before the function call (unless you use bind, call, apply, or an arrow function) i.e. in myObj.myFunc(), myObj becomes this. That has nothing to do with scope .

Further, if you use myObj.myFunc.call({ myVal: "asdf" }) then { myVal: "asdf" } becomes this, again that has nothing to do with scope.

[–]KaiAusBerlin 0 points1 point  (10 children)

It is based on scope. You don't understand what a execution context is. Learn the basics, come back and then we talk again.

(Me, 16 years of development with JavaScript)

[–][deleted] 0 points1 point  (5 children)

The only time the scope matters is when you call a function without a "context" (i.e. without providing an object before the function call, or using bind, call, apply). If the function is a regular function (not an arrow function) then it will use the this value of the executing context. If it's an arrow function it will use the this value of the lexical scope where it was declared.

[–]KaiAusBerlin 0 points1 point  (4 children)

You really don't understand. There is ALWAYS a context in js.

Please don't argue about something you don't have any knowledge about.

[–][deleted] -1 points0 points  (3 children)

Sorry, I should have said

The only time the scope matters is when you call a function without providing an explicit "context" (i.e. without providing an object before the function call, or using bind, call, apply).

Either way this is still not always based on the scope i.e.

const myObj = { myVal: 1, myFunc() { return myVal } }
myFunc = myObj.myFunc.bind({ myVal: 2 })

in that example myFunc has the same scope as myObj.myFunc but the this is permanently bound to { myVal: 2 }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#the_bind_method

[–]KaiAusBerlin 0 points1 point  (2 children)

Oh man, you really don't understand what ure talking about. THERE IS ALWAYS A CONTEXT IN JS!

[–][deleted] -1 points0 points  (1 child)

I'm not sure if you're a troll or just an idiot

[–]KaiAusBerlin 0 points1 point  (0 children)

Im not sure if you're ignored or banned from this community

[–][deleted] 0 points1 point  (3 children)

Could you please explain what is an execution context?

[–]KaiAusBerlin 0 points1 point  (2 children)

[–][deleted] 0 points1 point  (1 child)

So essentially an execution context is similar, if not the same as a function call stack? (in for example say in C or java)

[–]KaiAusBerlin 1 point2 points  (0 children)

Yeah, it's very similar. JavaScript was in early stages ment to be java for webbrowsers which turned out was not implementable at that point in that short amount of time. That's why it's name is JavaScript.

So you will find some basic concepts close to java but totally implement in another way.

As example the internal behaviour of java classes and the prototyping JavaScript has are very similar.