Skip to content

Instantly share code, notes, and snippets.

@eshacker
Created December 19, 2015 01:22
Show Gist options
  • Select an option

  • Save eshacker/de65b5d720f533a7f143 to your computer and use it in GitHub Desktop.

Select an option

Save eshacker/de65b5d720f533a7f143 to your computer and use it in GitHub Desktop.
Playing with obj reference in javascript
// Using stricter env
'use strict';
// our object; does only one thing.
var obj = {
getMe: function() {
return this;
}
};
// Yes, obj.getMe is a method that returns 'obj'/itself.
console.log("same reference", obj.getMe() === obj);
// But this way it wont.
console.log("different reference, thus: ", (0, obj.getMe)() === obj);
// Oh bye the way, both functions are similar.
// It's the reference that matters here, not the syntax.
console.log("function def", obj.getMe.toString());
console.log("function def", (0, obj.getMe).toString());
// using a method as a function.
var getter = obj.getMe;
// yup, it wouldn't be true, since reference is not present.
console.log("calling through function, diff ref", getter() === obj);
// even though the function def is same.
console.log("function def", getter.toString());
// we might trying to pass obj, like below, but we will fail
console.log("first thingy is discarded, thus: ", (obj, getter)() === obj);
// so why not `call` it with a reference of our own ?
console.log("but `call`'s first arg is ref", getter.call(obj) === obj);
// let's create another object and pass that as the reference.
var myObj = {
getMe: function() {
console.log('myObj evoked');
return this;
},
};
// this will work
console.log("yup diff ref works in func", getter.call(myObj) === myObj);
// this will not
console.log("this ought to fail", getter.call(myObj) === obj);
// Wait this was firebug's console.
// Here's what chrome's console has to say.
var log = console.log;
// doesn't work
log("shouldn't work", "hi from console.");
// but this does
var log = console.log.bind(console);
log("should work", "hi from console.");
VM112:4 Uncaught TypeError: Illegal invocation
at <anonymous>:4:1
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)(anonymous function) @ VM112:4InjectedScript._evaluateOn @ VM82:875InjectedScript._evaluateAndWrap @ VM82:808InjectedScript.evaluate @ VM82:664
log = console.log
log() { [native code] }
log('hi')
VM205:2 Uncaught TypeError: Illegal invocation(…)(anonymous function) @ VM205:2InjectedScript._evaluateOn @ VM82:875InjectedScript._evaluateAndWrap @ VM82:808InjectedScript.evaluate @ VM82:664
log = console.log.bind(console)
log() { [native code] }
log('hi')
VM362:2 hi
undefined
var logger1 = console.log
undefined
var logger2 = console.log.bind(console);
undefined
logger1 === logger2
false
logger1.toString() === logger2.toString()
false
logger1.toString()
"function log() { [native code] }"
logger2.toString()
"function () { [native code] }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment