Skip to content

Instantly share code, notes, and snippets.

@mitsuru793
Last active December 2, 2015 06:04
Show Gist options
  • Select an option

  • Save mitsuru793/bd548c802340c304cfc1 to your computer and use it in GitHub Desktop.

Select an option

Save mitsuru793/bd548c802340c304cfc1 to your computer and use it in GitHub Desktop.
JavaScriptでnewを付けない関数だと、thisはグローバルオブジェクトになる。
// console.logのエイリアス
var l = function(x) { console.log(x) };
function Person(name) {
l(this);
this.name = name
}
Person("田中"); // Window
l(name); // 田中
l(this); // Window
l(this.name); // 田中
l("--suzuk--")
suzuki = new Person("鈴木"); // Person {}
l(suzuki.name); // 鈴木
l(name); // 田中
l(this); // Window
l(this.name); // 田中
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment