Created
December 21, 2018 11:43
-
-
Save qkreltms/38b6f11c9d303a1925ba709f72217318 to your computer and use it in GitHub Desktop.
javascript: method vs function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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