关于js对象的一些认识:
1.对象分为普通对象和函数对象
普通对象和函数对象都具有原型链__proto__属性,但函数对象还有一个原型propotype属性
2.一般地函数的原型是object 如var f=function(){};typeof(f.prototype)==object 但是Function.propotype却任然为函数 这点比较特殊
------------------------------
那原型对象是用来做什么的呢?主要作用是用于继承。(其实我个人认为是为了防止原来对象的属性和方法被继承过来的东西污染)举了例子:
var person = function(name){
this.name = name
};
person.prototype.getName = function(){
return this.name;
}
var zjh = new person(‘zhangjiahao’);
zjh.getName(); //zhangjiahao
从这个例子可以看出,通过给person.prototype设置了一个函数对象的属性,那有person实例(例中:zjh)出来的普通对象就继承了这个属性。
从这里可以看出person.prototype就是一个桥接的作用(类似于接口的作用 实现父类属性或方法的拓展),创造一些person函数中没有的属性然后再赋给person的实例
但这里需要注意的是 通过person.prototype创造的属性和方法 person是不具备的!!!和接口类似,接口的方法父类也不具备(实现父类属性或方法的拓展)同样的person.prototype也不可以直接点出person的属性 但可以通过使用this 间接点出如:
function A(name){
this.name = name;
};
A.prototype.sayName = function(){
console.log(this.name);
return this.name;//这里的this仍然指向A
}
但是person的实例化可以同时拥有person 的属性和person原型的属性
-----------------------------------------------
函数实例化的原型链===函数的原型
如:var a=new F1();
则a.__proto__===F1.prototype
----------------------------------------------------
注意:
1. Dog.prototype.say = function(){
return this.name;
}
2. Dog.prototype = {
say:function(){
return this.name
}
};
表面看起来1与2都是给Dog原型创造了一个函数属性,但他们还是有区别的
1的Dog.prototype引用依然是Dog 但是2的原型引用变成了Object!!这点需要注意,为了让2的引用重新变回Dog 可以手动重新赋值
如Dog.peototype.constructor = Dog;
function extend(Child,Parent){
var F = function(){};
F.prototype = Prent.prototype;
Child.prototype = new F;//此时Child.prototype.constructor为F
Child.prototype.constructor = Child;//让Child.prototype的引用重新变回Child
}
--------------------------------------------
if(arguments[0].constructor ==Number){}
arguments[0].constructor ==Number 表示的是输入的参数引用的类型为Number(js的内置对象函数常用的有Boolean Math String Date Array Number Function)
其中argument为内置参数对象 获得的是数组
主要是配合js函数重载
比如::
function showDate(){
if(arguments.length==0){
var date=new Date().toLocaleDateString();
return "您没有输入参数,现在时间:"+date ;
}
if(arguments.length==1){}
if(arguments[0].constructor ==Date){
return "您输入的参数是Date类型,现在时间是:"+arguments[0].toDateString();
}
}
1.对象分为普通对象和函数对象
普通对象和函数对象都具有原型链__proto__属性,但函数对象还有一个原型propotype属性
2.一般地函数的原型是object 如var f=function(){};typeof(f.prototype)==object 但是Function.propotype却任然为函数 这点比较特殊
------------------------------
那原型对象是用来做什么的呢?主要作用是用于继承。(其实我个人认为是为了防止原来对象的属性和方法被继承过来的东西污染)举了例子:
var person = function(name){
this.name = name
};
person.prototype.getName = function(){
return this.name;
}
var zjh = new person(‘zhangjiahao’);
zjh.getName(); //zhangjiahao
从这个例子可以看出,通过给person.prototype设置了一个函数对象的属性,那有person实例(例中:zjh)出来的普通对象就继承了这个属性。
从这里可以看出person.prototype就是一个桥接的作用(类似于接口的作用 实现父类属性或方法的拓展),创造一些person函数中没有的属性然后再赋给person的实例
但这里需要注意的是 通过person.prototype创造的属性和方法 person是不具备的!!!和接口类似,接口的方法父类也不具备(实现父类属性或方法的拓展)同样的person.prototype也不可以直接点出person的属性 但可以通过使用this 间接点出如:
function A(name){
this.name = name;
};
A.prototype.sayName = function(){
console.log(this.name);
return this.name;//这里的this仍然指向A
}
但是person的实例化可以同时拥有person 的属性和person原型的属性
-----------------------------------------------
函数实例化的原型链===函数的原型
如:var a=new F1();
则a.__proto__===F1.prototype
----------------------------------------------------
注意:
1. Dog.prototype.say = function(){
return this.name;
}
2. Dog.prototype = {
say:function(){
return this.name
}
};
表面看起来1与2都是给Dog原型创造了一个函数属性,但他们还是有区别的
1的Dog.prototype引用依然是Dog 但是2的原型引用变成了Object!!这点需要注意,为了让2的引用重新变回Dog 可以手动重新赋值
如Dog.peototype.constructor = Dog;
function extend(Child,Parent){
var F = function(){};
F.prototype = Prent.prototype;
Child.prototype = new F;//此时Child.prototype.constructor为F
Child.prototype.constructor = Child;//让Child.prototype的引用重新变回Child
}
--------------------------------------------
if(arguments[0].constructor ==Number){}
arguments[0].constructor ==Number 表示的是输入的参数引用的类型为Number(js的内置对象函数常用的有Boolean Math String Date Array Number Function)
其中argument为内置参数对象 获得的是数组
主要是配合js函数重载
比如::
function showDate(){
if(arguments.length==0){
var date=new Date().toLocaleDateString();
return "您没有输入参数,现在时间:"+date ;
}
if(arguments.length==1){}
if(arguments[0].constructor ==Date){
return "您输入的参数是Date类型,现在时间是:"+arguments[0].toDateString();
}
}