答:基本数据类型有string,null,number,symbol,undefined,boolean。
答:string是基本数据类型,object是引用数据类型。基本数据类型是储存于栈中,引用数据类型存储于堆中。
string用于表示0或者多个16位unicode字符组成的字符序列。对象就是一组数据和功能的集合。
function test(a,b){
return a-b;
}
var test2 = (a,b)=>{
return a-b;
}
// function的this指向在调用的时候才知道指向谁,是不固定的
function test(){
console.log(this);
}
test(); //Window {postMessage: ?, blur: ?, focus: ?, close: ?, frames: Window, …}
var obj = {aa:test};
obj.aa(); //{aa: ?}
// 箭头函数没有自己的this,它里面的this是继承所属上下文中的this,所以this指向是固定的
var test1 = () =>{
console.log(this);
}
test1(); //Window {postMessage: ?, blur: ?, focus: ?, close: ?, frames: Window, …}
var obj1 = {bb:test1};
obj1.bb(); //Window {postMessage: ?, blur: ?, focus: ?, close: ?, frames: Window, …}
function test(){
console.log(‘1‘);
}
let t = new test(); //1
//箭头函数没有prototype,constructor在prototype中,所以不能使用new生成构造函数
var test1 = ()=>{
console.log(‘2‘);
}
let t1 = new test1(); //test1 is not a constructor
test(); //1
function test(){
console.log(‘1‘);
}
test1(); //Uncaught TypeError: test1 is not a function
//用箭头函数定义函数的时候,需要var(let const定义的时候更不必说)关键词,而var所定义的变量不能得到变量提升,故箭头函数一定要定义于调用之前!
function test1(){
console.log(‘2‘);
}
new一个实例的过程
function New(fc){
//1.创建一个新的对象
let newObj = {};
//2.将对象的_proto_指向函数的prototype
if(fc.prototype!==null){
newObj.__proto__=fc.prototype;
}
//3.调用apply或者call方法,将步骤1新创建的对象作为this的上下文
let res = fc.apply(newObj,Array.prototype.slice.call(arguments,1));
if((typeof res === ‘object‘|| typeof res === ‘function‘) && res!==null){
return res;
}
//4.如果该函数没有返回对象,则返回this
return newObj;
}
function A(){
return 1;
}
var B = () => {
return 1;
}
console.log(New(A))
console.log(New(B)) // 箭头函数没有自己的原型prototype,没有this指向
回调函数,把函数作为参数传给另一个函数进行调用,一般来说,只要参数是一个函数,那么这个函数就是回调。
$button.on(‘click‘, function(){})
call和apply 都是用来修改函数中this的指向问题
不同的是传参方式,call方法可以传多个参数,apply方法只能将多个参数合成一个数组作为参数
var name = ‘windowName‘
var age = ‘windowAge‘
var person = {
name : ‘personName‘,
age : ‘personAge‘,
getPerson : function(){
return this.name + ‘ ‘ + this.age;
},
setPerson : function(name, age){
return name + ‘ ‘ + age;
}
}
var person1 = {
name : ‘person1Name‘
}
console.log(person.getPerson()); //personName personAge
// 使用call和apply,不传任何参数,默认指向windows
console.log(person.getPerson.call())//windowName windowAge
console.log(person.getPerson.apply())//windowName windowAge
// 有参数时,this指向第一个参数
console.log(person.getPerson.call(person))//personName personAge
console.log(person.getPerson.apply(person1))//person1Name undefined
// 当需要传递参数时,call可以直接写多个参数,apply需要用数组方式传递
console.log(person.setPerson.call(person,‘callName‘,‘callAge‘)) //callName callAge
console.log(person.setPerson.apply(person1,[‘applyName‘,‘applyAge‘])) //applyName applyAge
//当参数超过3个时,call会比apply性能好
//call 方法比 apply 快的原因是方法的参数格式正是内部方法所需要的格式。
减少网络连接
减小资源大小
优化网络连接
<link rel=‘dns-prefetch‘ href=‘www.baidu.com‘>
优化资源加载
减少重绘回流
样式设置(避免使用很多层的选择器,减少使用table布局;避免使用CSS表达式;尽量给图片、元素设置宽高;使用css实现效果而不使用js)
DOM优化
缓存DOM节点
let div = document.getElementById(‘app‘)
减少DOM的深度和数量
批量操作DOM或者CSS样式
防抖(debounce)和节流(throttle)
function debounce(fc,wait){
let timer;
return function(){
var self = this;
if(timer!==null)
clearTimeout(timer);
timer = setTimeout(function(){
fc.apply(self);
},wait)
}
}
function throttle(fc,wait){
let canRun = true;
return function(){
if(canRun){
canRun = false;
fc.apply(this,arguments);
serTimeout(function(){
canRun = true;
},wait)
}
}
}
使用较好的API
webpack优化
原文:https://www.cnblogs.com/hyj-/p/14981028.html