Open kgneng2 opened 4 years ago
function nonStrictMode() { return this } function strictMode() { 'use strict' return this } console.log(nonStrictMode()) // window console.log(strictMode()) // undefined console.log(window.stricMode()) // window
function NewObject(name, color) { this.name = name this.color = color this.isWindow = function() { return this === window } } const newObj = new NewObject('nana', 'yellow') console.log(newObj.name) // nana console.log(newObj.color) // yellow console.log(newObj.isWindow()) // false const newObj2 = new NewObject('didi', 'red') console.log(newObj2.name) // didi console.log(newObj2.color) // red console.log(newObj2.isWindow()) // false
const withoutNew = NewObject('nana', 'yellow') console.log(withoutNew.name) // error console.log(withoutNew.color) // error console.log(withoutNew.isWindow()) // error
function Family(firstName) { this.firstName = firstName const names = ['bill', 'mark', 'steve'] names.map(function(lastName, index) { console.log(lastName + ' ' + this.firstName) console.log(this) }) } const kims = new Family('kim') // bill undefined // window // mark undefined // window // steve undefined // window
function Family(firstName) { this.firstName = firstName const names = ['bill', 'mark', 'steve'] const that = this names.map(function(value, index) { console.log(value + ' ' + that.firstName) }.bind(this)) } const kims = new Family('kim')
function Family(firstName) { this.firstName = firstName const names = ['bill', 'mark', 'steve'] names.map((value, index) => { console.log(value + ' ' + this.firstName) }) } const kims = new Family('kim')
This 는 현재 실행 문맥이다
생성자 함수/ 객체
생성자 함수 안에 다른 함수 있을때
해결법
결론
reference