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
};
})();