面试题总结

来源:互联网 发布:淘宝退货邮费谁承担 编辑:程序博客网 时间:2024/06/06 00:47

1、写一个闭包的实现

2、如何让将js闭包内部局部变量全部删除?
var foo = (function() {    var n = 0;    return {        add: function () {            return ++n;        },        clearVariable: function () {            n = null;        }    }})();
外部是无法访问到闭包里边的变量,但可以在闭包内部返回一个方法,该方法将闭包内部的变量设置为null,让变量失去引用,会被系统自动回收。

Javascript 闭包,引用的变量是否被回收?

https://www.zhihu.com/question/40678847/answer/87982345


3、原型链继承 中constructor有什么作用?
function Person(area){  this.type = 'person';  this.area = area;}Person.prototype.sayArea = function(){  console.log(this.area);}var Father = function(age){  this.age = age;} Father.prototype = new Person('Beijin');console.log(Person.prototype.constructor) //function person()console.log(Father.prototype.constructor); //function person()Father.prototype.constructor = Father;//修正console.log(Father.prototype.constructor); //function father()var one = new father(25);

首先,constructor是原型对象上的一个属性,默认指向这个原型的构造函数。

当你没有进行斧正的时候,

var one = new Father(25)
console.log(one.constructor) // function Person() {}

Father构建出来的对象one的构造函数指向是Person,而不是你new它的时候用的构造函数Father。这就导致了原型链的错乱。

因为在

Father.prototype = new Person('Beijin');

这一步的时候,原型指向了一个新对象,这个新对象的constructor指向的是Person

Father.prototype.constructor = Father; // 修正

这一步的修正,也就相当于在new Person()的对象上添加一个constructor属性,重新指向Father,保证原型链的正确。

4、

factory、service、provider区别

http://blog.csdn.net/zcl_love_wx/article/details/51404390
58一面
1、数组去重,其中有1和“1”
(for循环、filter、es6的)
原创粉丝点击