原型链和构造函数 20/5/26 2:14 PM
1、准确判断一个变量是数组类型
var arr = []
arr instanceof Array // true
typeof arr //Object
说明 typeof 是无法判断数组的
typeof 一般返回如下结果:
number Boolean string function object(NULL、数组、对象)
unde+ned
typeof(null)// Object
2、因为 Array 是 Object 的子类,所以
arr instanceof Object //true
再如:
function Foo(){};
var foo=new Foo();
foo instanceof Foo //true。
3、原型链继承的实例
function Name(){
this.name = function(){
console.log(‘cb’)
}
}
function Firstname() {
this.+rstname = function () {
console.log("L")
}
}
Name.prototype = new Firstname();
var he = new Name();
he.name(); //’cb’
he.+rstname();//’L’
4、__proto__为隐式原型,prototype 为显示原型
5、String 原型链实例
String.prototype.formData = function (){
return 'hhhhhh'
}
'sss'.formData() //"hhhhhh"
评论0
最新资源