1.
var a = 90;
function Person() {
var a = 100;
this.x = 100;
this.say = function () {
console.log(this.x)
console.log(this.y)
fn(this)
}
function fn(arg) {
console.log(arg)
console.log(this)
console.log(a)
}
}
var a = { x: 90, y: 666, fm: function () { console.log(this) } }
Person.prototype = a;
var p1 = new Person()
console.log(p1)
p1.say()
p1.fm()
这道题主要考的是作用域。在些这种题是就把自己当做计算机就行。
形实函运,记住一些优先级一步步运行下来即可。
2.
function Parent(){
this.a=1;
this.b=[1,2,this.a];
this.c={demo:5}
this.show=function(){
console.log(this.a,this.b,this.c.demo)
}
}
function Child(){
this.a=2;
this.change=function(){
this.b.push(this.a);
this.a=this.b.length;
this.c.demo=this.a++;
}
}
Child.prototype=new Parent();
var parent =new Parent();
var child1=new Child();
var child2=new Child();
child1.a=11;
child2.a=12;
parent.show();
child1.show();
child2.show();
child1.change();
child2.change();
parent.show();
child1.show();
child2.show();
一步一步向下运行即可,我就是计算机。
3.
function fn(){
rank = function(){console.log("盖伦")}
console.log(this);
return this
}
fn.rank =function(){
console.log("赵信")
}
fn.prototype.rank=function(){
console.log("典韦")
}
var rank=function(){
console.log("项羽")
}
function rank(){
console("琴女")
}
fn.rank()
rank()
fn().rank()
rank()
new fn.rank()//new (fn.rank))
new fn().rank()//(new fn()).rank()
new new fn().rank()//new(new fn()).rank()
把优先级搞懂就看能看懂,当你能把代码看成数学问题时说明你已经懂了。
4.
function Person() { }
Person.prototype = {
greet: function () {
console.log('Hello');
}
};
var person = new Person();
Person.prototype = {
greet: function () {
console.log('Hola');
}
};
person.greet(); // 输出什么?
最后来个简单顺序问题。