3月份是找工作的高峰期,最近也面試了很多前端,然后本人也不是什么技術大牛,很多時候都不知道該從那些方面去考察一個人的技術水平,希望這篇文章能夠拋磚引玉,聽聽各位大神的意見,那么就來說說我面試前端主要問些什么吧。
一、JavaScript的對象。
此外,JavaScript 允許自定義對象,有兩種不同的方法:
- 定義並創建對象的實例
- 使用函數來定義對象,然后創建新的對象實例
1、創建直接的實例
person=new Object(); person.firstname="Bill"; person.lastname="Gates"; person.age=56; person.eyecolor="blue";
這個例子創建了對象的一個新實例,並向其添加了四個屬性;此外,我們也可以通過對象字面量直接創建對象實例:person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
2、使用對象構造器
使用函數來構造對象:
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
二、JavaScript的面向對象。
function Animal(){
this.species = "動物";
}
還有一個"貓"對象的構造函數。
function Cat(name,color){
this.name = name;
this.color = color;
}
怎樣才能使"貓"繼承"動物"呢?
使用prototype屬性。
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛","黃色");
alert(cat1.species); // 動物
代碼的第一行,我們將Cat的prototype對象指向一個Animal的實例。
Cat.prototype = new Animal();
它相當於完全刪除了prototype 對象原先的值,然后賦予一個新值。但是,第二行又是什么意思呢?
Cat.prototype.constructor = Cat;
原來,任何一個prototype對象都有一個constructor屬性,指向它的構造函數。如果沒有"Cat.prototype = new Animal();"這一行,Cat.prototype.constructor是指向Cat的;加了這一行以后Cat.prototype.constructor指向Animal。
alert(Cat.prototype.constructor == Animal); //true
alert(cat1.constructor == Cat.prototype.constructor); // true
alert(cat1.constructor == Animal); // true
o.prototype = {};
o.prototype.constructor = o;
這里還有另外一種方法,就是直接繼承prototype,這種方法是對第一種方法的改進。由於Animal對象中,不變的屬性都可以直接寫入Animal.prototype。所以,我們也可以讓Cat()跳過 Animal(),直接繼承Animal.prototype。
function Animal(){ }
Animal.prototype.species = "動物";
然后,將Cat的prototype對象,然后指向Animal的prototype對象,這樣就完成了繼承。
Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛","黃色");
alert(cat1.species); // 動物
Cat.prototype.constructor = Cat;
alert(Animal.prototype.constructor); // Cat
三、上下文(this的理解)。
1、全局范圍內this;
2、函數調用
3、方法調用
4、調用構造函數
5、顯式的設置 this
function foo(a, b, c) {}
var bar = {};
foo.apply(bar, [1, 2, 3]); // 數組將會被擴展,如下所示
foo.call(bar, 1, 2, 3); // 傳遞到foo的參數是:a = 1, b = 2, c = 3
四、閉包
function a() {
var i = 0;
function b() {
alert(++i);
}
return b;
}
var c = a();
c();
閉包的應用場景
function Constructor(...) {
var that = this;
var membername = value;
function membername(...) {...}
}
五、Javascript模塊化開發的實現
var module1 = (function(){
var _count = 0;
var m1 = function(){
//...
};
var m2 = function(){
//...
};
return {
m1 : m1,
m2 : m2
};
})();
