-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?

Community
  • 1
  • 1
D_Pavel
  • 29
  • 5

1 Answers1

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
NiRUS
  • 3,901
  • 2
  • 24
  • 50