Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The lexical binding of an arrow-function cannot be overridden (even with new!) #1841

Closed
VovaSv opened this issue Sep 26, 2023 · 4 comments
Closed

Comments

@VovaSv
Copy link

VovaSv commented Sep 26, 2023

https://github.com/getify/You-Dont-Know-JS/blame/59d33b0c47c214270b87e7afd5670ad864d8a465/this%20%26%20object%20prototypes/ch2.md#L797

Please correctly if I'm wrong but in ch2 was said that "The lexical binding of an arrow-function cannot be overridden (even with new!)"

But in an example below the new foo() do override the lexical binding of an arrow-function, isn't true?

function foo() {
	console.log('foo this is :' , this)
	return (a) => {
      debugger;
		// `this` here is lexically adopted from `foo()`
		console.log('from arrow', this.a );
	};
}

var obj1 = {
	a: 2
};

var obj2 = {
	a: 3
};

foo.bind( obj1 );
var bar = new foo(); // new override lexical scope of arrow function
bar(); 
@VovaSv
Copy link
Author

VovaSv commented Sep 26, 2023

Wrong assumption.
In example above we actually override by new "new foo()" a this and not a lexical binding of arrow function which is actually a foo functions itself.

@VovaSv VovaSv closed this as completed Sep 26, 2023
@getify
Copy link
Owner

getify commented Sep 27, 2023

'new' is called on the foo function, not on the arrow function it returns (assigned to bar).

@VovaSv
Copy link
Author

VovaSv commented Sep 27, 2023

@getify So in simple words "The lexical binding of an arrow-function cannot be overridden (even with new!)" because we basically can't even call arrow function with new (as no constructor in arrow func)?

@getify
Copy link
Owner

getify commented Sep 27, 2023

correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants