javascript 面向对象基础知识:
//给object对象定义属性;
var obj = new Object;
obj.x = "100";
obj.y = "200";
//定义一个类;
function Foo(){
this.id = 300;
this.name = "zhouhaitao";
}
//定义一个数组;
var array = new Array();
//属性测试;
function testProperty(){
alert(obj.x);
alert(obj.y);
var foo = new Foo();
alert(foo.id);
alert(foo.name);
array.push("数组值1");
array.push("数组值2");
array.push("数组值3");
/**
* 循环;
*/
for(var inx in array){
alert(array[inx]);
}
}
//Object对象的inObj设置为1;
Object.prototype.inObj = 1;
//定义A类;
function A(){
//给属性设置值为2;
this.inA = 2;
}
//给A类的inAproto属性设置为3;
A.prototype.inAProto = 3;
B.prototype = new A();
B.prototype.constructor = B;
//定义B类;
function B(){
//给B类属性设置值为4;
this.inB = 4;
this.firstMethod = function (num1,num2){
var count = num1+num2;
return count;
};
}
B.prototype.inBroto = 5;
x = new B;
document.writeln(x.inObj+","+x.inA+","+x.inAProto+","+x.inB+","+x.inBroto);
//打印
function pirintMethod(){
//调用方法;
var count = x.firstMethod(1000,1000);
alert(count);
//定义一个新的方法;
B.secondMethod = function(msg){
return msg;
};
var msg = B.secondMethod("Hello");
alert("这是创建一个新的方法:"+msg);
}
//定义一个C类;
function C(){
this.x = 1;
this.eat = function(){
this.x=4;
return this.x;
};
}
//给C类定义吃的方法;
C.prototype.eat2=function(){
var y = new C().eat();
this.x = (y+=1);
};
//定义一个D类;
function D(){
this.x = 1;
this.eat = function(){
this.x=5;
};
}
//给D类定义一个吃的方法;
D.prototype.eat2 = function(){
this.x+=2;
};
c = new C();
d = new D();
c.eat2();
d.eat();
document.writeln("<br/>"+c.x+","+d.x+"</br>");
//给E类定义私有成员;
function E(){
var num = 100;
//定义getter方法;
this.getNum = function(){
return num;
};
//定义setter方法;
this.setNum = function(number){
num = number;
};
};
//测试;
function privateProperty(){
var e = new E();
//设置值;
e.setNum(500);
//获取值;
var result =e.getNum();
alert("私有属性:"+result);
myObjMethod();
}
//定义一个空对象;
var myobj = {};
myobj.name = "zhang san";
//定义一个get方法;
myobj.getName = function(){
return this.name;
};
//定义一个person对象;
var person={id:100,name:'Test',age:30,address:function(x){
return x;
}};