原型:
(1)每个函数|类都有个显示原型protptype
(2)每个实例都有一个隐式原型__proto__
(3)实例的隐式原型__proto__等于对应函数|类的显示原型prototype
原型链:
(1)当获取对象的属性|方法时,先在自身对象上查找,找不到则向上查找隐式原型
__proto__直到__proto__为null为止
(2)我们把由__proto__组成的链条关系称为原型链。
(3)js对象__proto__通过原型链实现链继承。
函数套函数,变量作用域的特殊情况
函数被当作参数传入,函数被作为返回值
闭包中的自由变量是在函数定义的时候确定的。被闭包引用的局部变量不会被撤销
作用:封装,实现局部变量,避免全局变量污染。
jQuery是如何实现无new化操作?
function JQ(){
return new JQ.prototype.init()
}
JQ.prototype.init=function(){return this}
JQ.prototype.init.prototype=JQ.prototype
typeof方法
<script>
var a = 123;
var b = "abc";
var c = false;
var d;
var e = {};
var f = [];
var h = null;
function fun() {}
console.log(typeof(a)) //number
console.log(typeof(b)) //string
console.log(typeof(c)) //boolean
console.log(typeof(d)) //undefind
console.log(typeof(e)) //object
console.log(typeof(fun)) //funcation
console.log(typeof(null)) //object
console.log(typeof(NaN)) //number
console.log(typeof(f)) //object
</script>
Object.prototype.toString.call([]);
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number、string、undefined、boolean、object。
对于null、array、function、object来说,使用typeof都会统一返回object字符串。
要想区分对象、数组、函数、单纯使用typeof是不行的。在JS中,可以通过Object.prototype.toString方法,判断某个对象之属于哪种内置类型。
分为null、string、boolean、number、undefined、array、function、object、date、math。
1. 判断基本类型
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(“abc”);// "[object String]"
Object.prototype.toString.call(123);// "[object Number]"
Object.prototype.toString.call(true);// "[object Boolean]"
2. 判断原生引用类型
//函数类型
Function fn(){
console.log(“test”);
}
Object.prototype.toString.call(fn); // "[object Function]"
//日期类型
var date = new Date();
Object.prototype.toString.call(date); // "[object Date]"
//数组类型
var arr = [1,2,3];
Object.prototype.toString.call(arr); // "[object Array]"
//正则表达式
var reg = /[hbc]at/gi;
Object.prototype.toString.call(reg); // "[object RegExp]"
//自定义类型
function Person(name, age) {
this.name = name;
this.age = age;
}
var person = new Person("Rose", 18);
Object.prototype.toString.call(arr); // "[object Object]"
instance of 是否为xx实例 如果在原型链__proto__ 有右侧对象就会返回true
var o = {
'name':'lee'
};
var a = ['reg','blue'];
console.log(o instanceof Object);// true
console.log(a instanceof Array);// true
console.log(o instanceof Array);// false
constructor方法
var a = {
'name':'hu'
};
var b = ['reg','blue'];
console.log(a.constructor == Object);//true
console.log(b.constructor == Array);//true
isArray 判断是否数组
var a = [1,2,3];
console.log(typeof a); //返回“object”
console.log(Array.isArray(a)); //true
函数递归就是自己调用自己的方法
<script>
function app(n){
if(n<=1){return 1}
return n+app(n‐1)
}
app(20)
<body>
<ul id="box">
</ul>
<script type="text/javascript">
for(var i = 1 ; i<=10; i++){
var ul = document.getElementById('box');
var p=document.createElement('p')
p.innerHTML=`第${i}行`
ul.appendChild(p);
console.log(i);
(function(i){
p.onclick=function(){
alert(i)
}
})(i)
}
</script>
</body>
vue双向绑定原理?
vue.js是采用数据劫持结合发布者-订阅者模式的方式,通过object.defineProperty()来劫
持个个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回调!
watch、computed,methods区别?
wacth监听数据变化执行handler函数,computed是从现有数据计算出新的数据,
computed会缓存,wacth不会
comouted不能实现异步,watch不能
computed可是多对一关系(多个数据,计算一个数据)watch1对多的关系
watch没有缓存频繁操作,会严重影响性能。