面试题_原型vs原型链

214 阅读1分钟
  1. 最好画图

    function Fn() {
        this.x = 100;
        this.y = 200;
        this.getX = function () {
            console.log(this.x);
        }
    }
    Fn.prototype.getX = function () {
        console.log(this.x);
    };
    Fn.prototype.getY = function () {
        console.log(this.y);
    };
    let f1 = new Fn;
    let f2 = new Fn;
    console.log(f1.getX === f2.getX);
    console.log(f1.getY === f2.getY);
    console.log(f1.__proto__.getY === Fn.prototype.getY);
    console.log(f1.__proto__.getX === f2.getX);
    console.log(f1.getX === Fn.prototype.getX);
    console.log(f1.constructor);
    console.log(Fn.prototype.__proto__.constructor);
    f1.getX();
    f1.__proto__.getX();
    f2.getY();
    Fn.prototype.getY();
    
  2. 一到经典的面试题

    function C1(name) {
        if (name) {
            this.name = name;
        }
    }
    function C2(name) {
        this.name = name;
    }
    function C3(name) {
        this.name = name || 'join';
    }
    C1.prototype.name = 'Tom';
    C2.prototype.name = 'Tom';
    C3.prototype.name = 'Tom';
    alert((new C1().name) + (new C2().name) + (new C3().name));
    
  3. 计算输出的结果

    function Fn(num) {
    	this.x = this.y = num;
    }
    Fn.prototype = {
    	x: 20,
    	sum: function () {
    		console.log(this.x + this.y);
    	}
    };
    let f = new Fn(10);
    console.log(f.sum === Fn.prototype.sum);
    f.sum();
    Fn.prototype.sum();
    console.log(f.constructor);
    
  4. 计算输出的结果

    function Fn() {
    	let a = 1;
    	this.a = a;
    }
    Fn.prototype.say = function () {
    	this.a = 2;
    }
    Fn.prototype = new Fn;
    let f1 = new Fn;
    
    Fn.prototype.b = function () {
    	this.a = 3;
    };
    console.log(f1.a);
    console.log(f1.prototype);
    console.log(f1.b);
    console.log(f1.hasOwnProperty('b'));
    console.log('b' in f1);
    console.log(f1.constructor == Fn);
    
  5. 编写两个方法 plus / minus 实现如下的执行效果

    let n = 10;
    let m = n.plus(10).minus(5);
    console.log(m);//=>15(10+10-5)
    
  6. 阿里超经典面试题

    function Foo() {
        getName = function () {
            console.log(1);
        };
        return this;
    }
    Foo.getName = function () {
        console.log(2);
    };
    Foo.prototype.getName = function () {
        console.log(3);
    };
    var getName = function () {
        console.log(4);
    };
    function getName() {
        console.log(5);
    }
    Foo.getName();
    getName();
    Foo().getName();
    getName();
    new Foo.getName();
    new Foo().getName();
    new new Foo().getName();
    

7、输出下面代码运行结果

//example 1
var a={}, b='123', c=123;  
a[b]='b';
a[c]='c';  
console.log(a[b]);

---------------------
//example 2
var a={}, b=Symbol('123'), c=Symbol('123');  
a[b]='b';
a[c]='c';  
console.log(a[b]);

---------------------
//example 3
var a={}, b={key:'123'}, c={key:'456'};  
a[b]='b';
a[c]='c';  
console.log(a[b]);

8、输出结果

function Foo() {
    Foo.a = function() {
        console.log(1)
    }
    this.a = function() {
        console.log(2)
    }
}
Foo.prototype.a = function() {
    console.log(3)
}
Foo.a = function() {
    console.log(4)
}
Foo.a();
let obj = new Foo();
obj.a();
Foo.a();