问一个JS function.apply()的问题
高洛峰
高洛峰 2016-11-19 13:19:00
0
2
656
function Person(a, b ,c) {
  this.a = a;
  this.b = b;
  this.c = c
}


function Child() {
  Person.apply(null, [1, 2, 3])
}

console.log(window.a)

网上搜了下如果apply第一个参数传入null或者undefined,指针指向全局,但全局window.a打印出来是undefined,主要纠结如果apply方法第一个参数传入null或者undefined的情况,正常传入this的话,Child的实例能够渠道这个a值。传入null或者undefined指向全局,但能否举个在这个指向全局情况下a b c怎么取或者去哪了

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
学霸

执行一下Child()

function Person(a, b ,c) {
  this.a = a;
  this.b = b;
  this.c = c
}


function Child() {
  Person.apply(null, [1, 2, 3])
}

**Child()**
console.log(window.a)


三叔

哥你都没执行好么 ... 你的想法很对

function Person(a, b ,c) {
  this.a = a;
  this.b = b;
  this.c = c
}

function Child() {
  Person.apply(null, [1, 2, 3])
}

Child();

console.log(window.a) // 1


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!