1, 判断下列代码输出什么?
// 判断下列代码输出什么?
//
var myObject = {
foo: "bar",
func: function () {
var self = this;
console.log(this.foo); // 此处的this为myObject的this,故输出bar
console.log(self.foo); // 此处的self承接this,故输出bar
(function () {
console.log(this.foo); // 立即执行函数的this指向window对象, 而window上不存在foo故输出underfined
console.log(self.foo); // 此处要清楚立即执行函数的作用域内部无变量self,但是可通过作用域链向上查找,故找到父节点myObject的
// self 输出bar
}());
}
};
myObject.func();
2, JavaScript 的保留字有哪些?
3, 一下代码输出什么?
// 以下JS代码的输出是什么?
let A = function () { }
A.prototype.a = 1; // 此步骤为在对象上创建a变量并且赋值为1
let B = new A();
A.prototype = {
b: 2,
c: 3
}
let C = new A();
A.prototype.d = 4;
console.log(B.a); // 1
console.log(B.b); // underfined
console.log(C.c); // 3
console.log(C.d); // 4
// 解析:
// let A = function () { }
// A.prototype.a = 1;
// //此时 A.prototype = { a: 1 }
// let B = new A(); //此时 B = {}
// //在创建B时,已将B._proto_ = A.prototype = { a: 1 }
// //即使后面A.prototype重新赋值,将A.prototype开辟了新的空间指向别的对象
// //B._proto_并没有改,还是指向{a:1}这个对象
// A.prototype = { //此时 A.prototype = { b: 2, c: 3 }
// b: 2,
// c: 3
// }
// let C = new A(); //C = {}
// A.prototype.d = 4;//此时A.prototype = { b: 2, c: 3, d: 4 }
// console.log(B.a); //1
// console.log(B.b); //undefined
// console.log(C.c); //3
// console.log(C.d); //4
//C.d
//着重区分: A.prototype.d = 4 和 A.prototype 重新赋值 不是一个概念
//A.prototype重新赋值时,A.prototype已经指向另一个对象了
//A.prototype.d = 4时,访问的还是同一个A.prototype 对象
4, 严格模式判断:
严格模式下构造函数其this指向实例化对象, 普通函数指向windows