function Student(name,age,from)
{
var origName = name;
this.name = FormatName(name);
this.age = age;
this.from = from;
var FormatName = function(name)
{
return name+".china";
}
this.ToString = function()
{
return "my information is name: "+origName+",age : "+this.age+", from :" +this.from;
}
}
var zfp = new Student("3zfp",100,"ShenZhen");
3、私有方法无法访问this指示的变量(公开变量),如下:
复制代码 代码如下:
function Student(basicinfo)
{
this.basicInfo = basicinfo;
var FormatInfo = function()
{
this.basicInfo.name = this.basicInfo.name+".china";
}
FormatInfo();
}
function BasicInfo(name,age,from)
{
this.name = name;
this.age = age;
this.from = from;
}
var zfp = new Student(new BasicInfo("3zfp",100,"ShenZhen"));
基本结论是,私有方法只能访问私有属性,私有属性在声明并赋值后可以在类的任何地方访问,
实例变量:
实例变量即某个实例对象所拥有的变量。如:
复制代码 代码如下:
function BasicInfo(name,age,from)
{
this.name = name;
this.age = age;
this.from = from;
}
var basicA = new BasicInfo("3zfp",100,"ShenZhen");
basicA.generalInfo = "is 3zfp owned object";
document.write("basicA's generalInfo is : "+ basicA.generalInfo+"<br>");
var basicB = new BasicInfo("zfp",100,"ShenZhen");
document.write("basicB's generalInfo is : "+ basicB.generalInfo+"<br>");
执行该代码后,我们将可以看到如下结果:
basicA's generalInfo is : is 3zfp owned object
basicB's generalInfo is : undefined
静态变量即为某个类所拥有的属性,通过 类名+"."+静态变量名 的方式访问该属性。如下可以做清晰的解释:
复制代码 代码如下:
function BasicInfo(name,age,from)
{
this.name = name;
this.age = age;
this.from = from;
}
BasicInfo.generalInfo = "is 3zfp owned object";
var basic = new BasicInfo("zfp",100,"ShenZhen");
document.write(basic.generalInfo+"<br>");
document.write(BasicInfo.generalInfo+"<br>");
BasicInfo.generalInfo = "info is changed";
document.write(BasicInfo.generalInfo+"<br>");
执行以上代码,将会得到如下结果:
undefined
is 3zfp owned object
info is changed
注意以下几点:
1、以 类名+"."+静态变量名 的方式来声明一个静态变量
2、静态变量并不属于类的某个实例对象所独有的属性,为对象的共享.
3、能以实例对象名+"."+静态变量名来访问。
全局变量:
全局变量即整个系统运行期间有效访问控制的变量,通常是在一个js代码开头定义,如:
复制代码 代码如下: