-2

Something like this, but not using __proto__:

var o = { i :2 }; var f1 = function(){ alert(f1.i); } f1.__proto__ = o; o.i = 3; f1(); //3 

This question have not an answer here: How to set the prototype of a JavaScript object that has already been instantiated?

8
  • 5
    When I see such questions I always wonder: why do you people intentionally try to make your code as hard for understanding and maintenance as possible? Commented Aug 21, 2015 at 7:53
  • No, the function may not already been declared. Commented Aug 21, 2015 at 8:20
  • @D_Pavel: You're trying to set the internal prototype of a function object that doesn't yet exist? Commented Aug 21, 2015 at 8:27
  • @D_Pavel: why "like this, but not using __proto__"? What are you trying to do here in the first place, what is your use case? Commented Aug 21, 2015 at 8:28
  • the function object may be already exist or not. Commented Aug 21, 2015 at 9:30

1 Answer 1

0

One method is using bind API. So your Javascript looks like this

var o = {i :2}; var f1 = function(){ alert(this.i); }.bind(o) o.i = 3; f1(); //3 
Sign up to request clarification or add additional context in comments.

2 Comments

bind not set the __proto__
@D_Pavel and in your question you did not ask to set it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.