1.测试的function对象
2.新建一个对象,遍历此对象的属性
3.结果
注:并没有取到nullValue,此时的obj.nullValue为undefined。
所以定义1个js的function,里面的属性要有初始值
使用typeof这个内置的方法可以测试obj的类型
要调用方法,使用 jsobj.methodA()或者 jsobj['methodA']()
4.给对象增加属性
结果
给对象增加属性,并不会影响其他的对象
5.给function增加属性
结果
结果:对function增加属性,会影响所有对象
总结:要定义function的属性,需要使用this.pro,并且要有初始值,否则获取不到此属性。
使用var 定义的变量相当于java的private,是私有的。
私有typeof测试对象的类型,已知的有 function,object,string,number
json和array 都是object
function jsObj(){
this.name ="";
this.sex ="";
this.age=0;
this.father = new person();
this.json = {};
this.array = ['1','2'];
this.nullValue;
this.methodA=function(){alert('i am methodA')};
}
2.新建一个对象,遍历此对象的属性
var jsobj = new jsObj();
for(pro in jsobj )resultValue += (pro+"="+jsobj[pro]+",type="+typeof(jsobj[pro])+"<br>");
3.结果
name=,type=string
sex=,type=string
age=0,type=number
father=[object Object],type=object
json=[object Object],type=object
array=1,2,type=object
methodA=function(){alert('i am methodA')},type=function
注:并没有取到nullValue,此时的obj.nullValue为undefined。
所以定义1个js的function,里面的属性要有初始值
使用typeof这个内置的方法可以测试obj的类型
要调用方法,使用 jsobj.methodA()或者 jsobj['methodA']()
4.给对象增加属性
var jsobj1 = new jsObj();
var jsobj2 = new jsObj();
jsobj1.newAttr='newAttr'
结果
jsobj1.newAttr = newAttr
jsobj2.newAttr = undefined
给对象增加属性,并不会影响其他的对象
5.给function增加属性
jsObj.prototype.newAttr2 = 'newAttr2';
结果
jsobj1.newAttr2 = newAttr2
jsobj2.newAttr2 = newAttr2
结果:对function增加属性,会影响所有对象
总结:要定义function的属性,需要使用this.pro,并且要有初始值,否则获取不到此属性。
使用var 定义的变量相当于java的private,是私有的。
私有typeof测试对象的类型,已知的有 function,object,string,number
json和array 都是object