Handling a reference to an instance method

I have a method inside an object which uses "this" to return one of its properties. I create an instance of that object then delete the object, then create a reference to that object’s method.

Now when I call first object.method it returns the property, but running the reference returns undefined as "this"’s context is not that of the object but global.

How is it possible to manipulate the object so that the reference would return the correct property (instead of undefined) without using .bind when declaring the reference to the object’s method or using globals.

let book = {title : "horror", getbookname : function () {return this.title} }
let silenthill = book;
book = null;
let newFunc = silenthill.getbookname;
console.log(silenthill.getbookname());
console.log(newFunc());

returns "horror" and "undefined"

Edit:
Fixed the syntax

103 thoughts on “Handling a reference to an instance method”

Leave a Comment