Skip to content

Instantly share code, notes, and snippets.

@qkreltms
Created December 21, 2018 11:43
Show Gist options
  • Select an option

  • Save qkreltms/38b6f11c9d303a1925ba709f72217318 to your computer and use it in GitHub Desktop.

Select an option

Save qkreltms/38b6f11c9d303a1925ba709f72217318 to your computer and use it in GitHub Desktop.
javascript: method vs function
//method는 오브젝트 안에서 f:function() {} 이런식으로 되어있는 것을 말하고, this가 메소드가 속해있는 객체를 가리킨다.
//function은 function f() {} 이런식으로 되어있는 것을말하고 this가 window를 가리킨다.
var a = {
methods: {
methodOfObject: function() {
console.log("callit!")
functionBelongsToWindow()
function functionBelongsToWindow() {
console.log("in functionBelongsToWindow()"+this) // window를 가리키는 this
}
},
method: function() {
console.log(this) //methods를 가리키는 this
this.methodOfObject()
}
}
}
a.methods.method()
// class 안에서
class Person {
returnThis() {
console.log(this) //Person을 참조
}
}
var p = new Person()
p.returnThis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment