基础数据类型和引用数据类型
// 实现typeof
function type(obj) {
return Object.prototype.toString.call(a).slice(8,-1).toLowerCase();
}
复制代码
// 实现instanceof
function instance(left,right){
left=left.__proto__
right=right.prototype
while(true){
if(left==null)
return false;
if(left===right)
return true;
left=left.__proto__
}
}
理解原型链是做什么的,也就是:实例.proto === 构造函数.prototype
Object.prototype.__proto__ === null // true
Function.prototype.__proto__ === Object.prototype // true
Object.__proto__ === Function.prototype // true
有个比较好的问题,可以思考下:
function F() {}
Object.prototype.b = 2;
Function.prototype.a = 1;
var f = new F();
console.log(f.a) // 1
console.log(f.b) // 2
console.log(F.a) // undefined
console.log(F.b) // 2
上面代码,为什么F.a是undefined?
function F() {}
Object.prototype.b = 2;
Function.prototype.a = 1;
var f = new F();
console.log(f.a) // undefined
console.log(f.b) // 2
console.log(F.a) // 1
console.log(F.b) // 2
上面代码,为什么f.a是undefined?
function F() {}
F.prototype.a = 1;
var f1 = new F()
F.prototype = {
a: 2
}
var f2 = new F()
console.log(f1.a) // 1
console.log(f2.a) // 2
继承的几种方式:
function SuperType() {
this.name = 'Yvette';
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.getName = function () {
return this.name;
}
function SubType() {
this.age = 18;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
let instance1 = new SubType();
instance1.colors.push('yellow');
console.log(instance1.getName());
console.log(instance1.colors); // ['red', 'blue', 'green', 'yellow']
let instance2 = new SubType();
console.log(instance2.colors); // ['red', 'blue', 'green', 'yellow']
缺点:
构造函数继承:
function SuperType(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
function SubType(name) {
SuperType.call(this, name);
}
let instance1 = new SubType('draven');
instance1.colors.push('yellow');
console.log(instance1.colors); // ['red', 'blue', 'green', 'yellow']
let instance2 = new SubType('ben');
console.log(instance2.colors); // ['red', 'blue', 'green']
优点:
function SuperType(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SuberType(name, age) {
SuperType.call(this, name);
this.age = age;
}
SuberType.prototype = new SuperType()
SuberType.prototype.constructor = SuberType
let instance1 = new SuberType('draven', 25);
instance1.colors.push('yellow');
console.log(instance1.colors); // ['red', 'blue', 'green', 'yellow']
instance1.sayName(); //draven
let instance2 = new SuberType('ben', 22);
console.log(instance2.colors); // ['red', 'blue', 'green']
instance2.sayName();//ben
缺点:
优点:
function SuperType(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SuberType(name, age) {
SuperType.call(this, name);
this.age = age;
}
SuberType.prototype = Object.create(SuperType.prototype)
SuberType.prototype.constructor = SuberType
let instance1 = new SuberType('draven', 25);
instance1.colors.push('yellow');
console.log(instance1.colors); //[ 'red', 'blue', 'green', 'yellow' ]
instance1.sayName(); //draven
let instance2 = new SuberType('ben', 22);
console.log(instance2.colors); //[ 'red', 'blue', 'green' ]
instance2.sayName();//ben
ES6继承:
class SuperType {
constructor(age) {
this.age = age;
}
getAge() {
console.log(this.age);
}
}
class SubType extends SuperType {
constructor(age, name) {
super(age); // 调用父类的constructor(age)
this.name = name;
}
}
let instance = new SubType(18, 'draven');
instance.getAge(); // 18
// 实现固定参数的curry
function add(a, b, c, d) {
return a + b + c + d
}
function curry(fn) {
const length = fn.length
let params = []
return function func() {
params = params.concat([].slice.call(arguments))
if (params.length === length) {
const res = fn.apply(null, params);
params = [];
return res;
} else {
return func;
}
}
}
const addCurry = curry(add);
console.log(addCurry(1, 2)(3, 4)); // 10
console.log(addCurry(2)(3)(4)(5)); // 14
复制代码
// 实现随意参数的柯理化
function add() {
let params = [].slice.call(arguments);
function func() {
params = params.concat([].slice.call(arguments))
return func;
}
func.toString = () => {
return params.reduce((a, b) => {
return a + b;
}, 0);
}
return func;
}
console.log(add(1, 2)(3, 4)); // 10
console.log(add(2)(3)(4)(5)); // 14
函数防抖和节流,都是控制事件触发频率的方法。
// 防抖
export function debounce(func, wait, immediate) {
let timeout, args, context, timestamp, result;
let nowTime = Date.now || function () {
return new Date().getTime();
};
const later = function () {
let last = nowTime() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function () {
context = this;
args = arguments;
timestamp = nowTime();
let callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
复制代码
// 节流
function throttle(fn, threshhold) {
let timeout
let start = new Date;
threshhold = threshhold || 160
return function () {
const context = this, args = arguments, curr = new Date() - 0
clearTimeout(timeout)//总是干掉事件回调
if (curr - start >= threshhold) {
fn.apply(context, args)
start = curr
} else {
//让方法在脱离事件后也能执行一次
timeout = setTimeout(function(){
fn.apply(context, args)
}, threshhold);
}
}
}
这部分主要考查对let和var的理解,变量提升等。
看下面这个代码的执行结果是什么?
var foo = {n: 1};
var bar = foo;
foo.x = foo = {n: 2};
bar = ?
foo = ?
上面的执行结果是:bar = {n:1,x:{n:2}}; foo={n:2};
a();
var a=3;
function a(){
alert(10)
}
alert(a)
a=6;
a()
上面的执行结果是:10 3 error;
最后的error是因为a不是个function;
与
隐式转换的步骤:主要搞明白在强等和双等的时候做了什么事情,也就好理解了。
强等会首先比较两边的类型是否相同,如果不同则直接返回false;如果类型相同的话,则是按照来判断的,我们来看下所引起的隐式转换。
一、首先看双等号前后有没有NaN,如果存在NaN,一律返回false。
二、再看双等号前后有没有布尔,有布尔就将布尔转换为数字。(false是0,true是1)
三、接着看双等号前后有没有字符串, 有三种情况:
1、对方是对象,对象使用toString()或者valueOf()进行转换;
2、对方是数字,字符串转数字;(前面已经举例)
3、对方是字符串,直接比较;
4、其他返回false
四、如果是数字,对方是对象,对象取valueOf()或者toString()进行比较, 其他一律返回false
五、null, undefined不会进行类型转换, 但它们俩相等
通常情况下我们认为,将一个对象转换为字符串要调用toString()方法,转换为数字要调用valueOf()方法
,但是真正应用的时候并没有这么简单,看如下代码实例:
let obj = {
name: "draven",
age: 28
}
console.log(obj.toString()); //[object Object]
同理,我们再看valueOf()方法:
let arr = [1, 2, 3];
console.log(arr.valueOf());//[1, 2, 3]
从上面的代码可以看出,valueOf()
方法并没有将对象转换为能够反映此对象的一个数字。相反,我们用toString()
let arr = [1, 2, 3];
console.log(arr.toString());//1,2,3
注:很多朋友认为,转换为字符串首先要调用toString()方法, 其实这是错误的认识,我们应该这么理解,调用toString()方法可以转换为字符串,但不一定转换字符串就是首先调用toString()方法。
我们看下下面代码:
let arr = {};
arr.valueOf = function () { return 1; }
arr.toString = function () { return 2; }
console.log(arr == 1);//true
let arr = {};
arr.valueOf = function () { return []; }
arr.toString = function () { return 1; }
console.log(arr == 1);//true
上面代码我们可以看出,转换首先调用的是valueOf(),假如valueOf()不是数值,那就会调用toString进行转换!
let arr = {};
arr.valueOf = function () { return "1"; }
arr.toString = function () { return "2"; }
console.log(arr == "1");//true
假如"1"是字符串,那么它首先调用的还是valueOf()。
let arr = [2];
console.log(arr + "1");//21
上面的例子,调用的是toString()
;因为arr.toString()
之后是2。
转换过程是这样的,首先arr会首先调用valueOf()方法,但是数字的此方法是简单继承而来,并没有重写(当然这个重写不是我们实现),返回值是数组对象本身,并不是一个值类型,所以就转而调用toString()方法,于是就实现了转换为字符串的目的。
说明
大多数对象隐式转换为值类型都是首先尝试调用valueOf()方法。但是Date对象是个例外,此对象的valueOf()和toString()方法都经过精心重写,默认是调用toString()方法,比如使用+运算符,如果在其他算数运算环境中,则会转而调用valueOf()方法。
let date = new Date();
console.log(date + "1"); //Sun Apr 17 2014 17:54:48 GMT+0800 (CST)1
console.log(date + 1);//Sun Apr 17 2014 17:54:48 GMT+0800 (CST)1
console.log(date - 1);//1460886888556
console.log(date * 1);//1460886888557
复制代码
举例巩固提高 下面我们一起来做做下面的题目吧!
let a;
console.dir(0 == false);//true
console.dir(1 == true);//true
console.dir(2 == {valueOf: function(){return 2}});//true
console.dir(a == NaN);//false
console.dir(NaN == NaN);//false
console.dir(8 == undefined);//false
console.dir(1 == undefined);//false
console.dir(2 == {toString: function(){return 2}});//true
console.dir(undefined == null);//true
console.dir(null == 1);//false
console.dir({ toString:function(){ return 1 } , valueOf:function(){ return [] }} == 1);//true
console.dir(1=="1");//true
console.dir(1==="1");//false
[] == 0 // true
上面的都可以理解了吗?最后一行代码结果是true的原因是什么?
es6
这部分考查对es6的掌握熟练度,新增的一些类型,语法,等等。推荐大家看一看阮一峰老师的es6的文章
// 实现bind
Function.prototype.myBind = function (context,...args) {
let self = this;
let params = args;
return function (...newArgs) {
self.call(context, ...params.concat(...newArgs))
}
}
var a = {
name: 'this is a'
}
function sayName() {
console.log(this.name, arguments)
}
let newfn = sayName.myBind(a, '1234', '5678')
newfn('1000', '2000')
复制代码
// 实现call
Function.prototype.myCall = function (context,...args) {
context.fn = this;
context.fn(...args)
delete context.fn;
}
var a = {
name: 'this is a'
}
function sayName() {
console.log(this.name, arguments)
}
sayName.myCall(a, '1234', '5678')
复制代码
// setTimeout 实现setInterval
function mySetInterval(fn, time) {
let timer = {};
function timeout() {
timer.t = setTimeout(() => {
fn();
timeout()
}, time)
}
timeout();
return timer;
}
function clearMyInterval(timer) {
clearTimeout(timer.t)
}
复制代码
promise考察点比较多,包括实现自己的promise和一些调用的知识点
推荐两篇文章:实现Promise和Promise题
vue基本
一、 vue的生命周期:beforeCreate、created、beforeMounte、mounted、beforeUpdate、updated、beforeDestory、destroyed;
二、 Vue组件通信:
3是Proxy+Reflect,2是Object.defineProperty;dom-diff的优化;componentApi等
聊聊webpack
// 洋葱圈模型
function compose(middleware) {
return function (context, next) {
let index = -1;
function dispatch(i) {
if (i <= index) {
return Promise.reject('err')
}
index = i;
let fn = middleware[i];
if(i === middleware.length) {
fn = next;
}
if (!fn) {
return Promise.resolve();
}
try {
return Promise.resolve(fn(context, function next() {
return dispatch(i + 1);
}))
} catch (e) {
return Promise.reject(e);
}
}
dispatch(0);
}
}
复制代码
// 从小到大排序:
function bubblingSort(list){
let temp;
for(let i=0; i<list.length; i++){
for(let j=i; j<list.length; j++){
if(list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
return list;
}
let res = bubblingSort([10, 8, 2, 23, 30, 4, 7, 1])
console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
复制代码
从小到大排序:
function selectSort(list){
let r,temp;
for(let j=0; j<list.length; j++){
for(let i = j+1; i<list.length; i++){
if(list[j] > list[i]){
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
return list;
}
let res = selectSort([10, 8, 2, 23, 30, 4, 7, 1])
console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
复制代码
整个排序过程为n-1趟插入,即先将序列中第1个记录看成是一个有序子序列,然后从第2个记录开始,逐个进行插入,直至整个序列有序。
function insertSort(list) {
let flag;
for(let index = 1; index < list.length; index++) {
flag = list[index];
let j = index - 1;
while (flag < list[j]) {
list[j + 1] = list[j]
j--;
}
list[j + 1] = flag;
}
return list;
}
let res = insertSort([10, 8, 2, 23, 30, 4, 7, 1])
console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
复制代码
排序过程:先取一个正整数d1 通过一次排序,将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可对这两部分记录进行排序,以达到整个序列有序。 1、什么是hybrid? 1.包括图片视频等内容的懒加载(IntersectionObserver的使用封装) 1、 解析HTML结构。 此问题网上有很多回答,属于自由发挥问题;回答的深度和广度能够看出本人的知识面。此处就不多说了。 复制代码function shellSort(list) {
const length = list.length;
let j, temp;
for (let d = parseInt(length / 2); d >= 1; d = parseInt(d / 2)) {
for (let i = d; i < length; i++) {
temp = list[i];
j = i - d;
while (j >= 0 && temp < list[j]) {
list[j + d] = list[j];
j -= d;
}
list[j + d] = temp;
}
}
return list;
}
let res = shellSort([10, 8, 2, 23, 30, 4, 7, 1])
console.log(res); // [1, 2, 4, 7, 8, 10, 23, 30]
复制代码
function quickSort(v,left,right){
if(left < right){
var key = v[left];
var low = left;
var high = right;
while(low < high){
while(low < high && v[high] > key){
high--;
}
v[low] = v[high];
while(low < high && v[low] < key){
low++;
}
v[high] = v[low];
}
v[low] = key;
quickSort(v,left,low-1);
quickSort(v,low+1,right);
}
}
let list = [10, 8, 2, 23, 30, 4, 7, 1]
quickSort(list, 0, 7)
console.log(list); // [1, 2, 4, 7, 8, 10, 23, 30]
复制代码
其他
其他
2、jsbridge是什么?如何实现?
3、hybrid开发需要注意什么? 复制代码
2.数据的预加载,纯h5的prefetch && 与端结合的预加载方案
3.js的按需加载(配合webpack的import().then()的split实现) 复制代码
2、 加载外部脚本和样式表文件。
3、 解析并执行脚本代码。
4、 构造HTML DOM模型。//ready
5、加载图片等外部文件。
6、 页面加载完毕。//load 复制代码
如果对你有帮助,请给个赞与关注