日期:2014-05-16  浏览次数:20456 次

javascript面向对象知识学习笔记二

??? javascript 面向对象知识学习笔记二:

?

//访问属性;
function myObjMethod(){
	var name = myobj.getName();
	alert(name);
	
	//调用person对象;
	alert(person.id+","+person.name+","+person.age+","+person.address("GuangDongShenZhen"));
}

//定义数组;
function myArray(){
	var arr = [];
	arr.push("zhangsan");
	arr.push("lisi");
	arr.push("wangwu");
	arr.push("zhaoliu");
	
	for(var i = 0;i<arr.length;i++){
		document.writeln(arr[i]);
	}
	//换行;
	document.writeln("<br/>");
	
	var array = new Array();
	array.push("zhouhaitao");
	array.push("lishizeng");
	array.push("xieguowei");
	for(var i = 0;i<array.length;i++){
		document.writeln(array[i]);
	}
}

//定义构造函数;
function myConstruct(name,age){
	var name = name;
	var age = age;
	
	//定义getter方法;
	this.getName = function(){
		return name;
	};
	//定义getter方法;
	this.getAge = function(){
		return age;
	};
}

//为构造方法赋值;
function setConstruct(){
	var construct = new myConstruct('zhangsan',100);
	alert("姓名是:"+construct.getName()+",年龄是:"+construct.getAge());
}

//创建对象objA;
var objA = {
	id:100,
	getName:function(){
		return 'My Name is ObjA';
	}
};

var objB={
	bol1:true,
	bol2:false
};

//继承对象;
function extend(parent,child){
	//让子类获取父类的方法;
	for(var i in parent){
		child[i] = parent[i];
	}
}

function testExtend(){
	extend(objA, objB);
	var name = objB.getName();
	alert(objB.bol1+","+objB.bol2+","+name+","+objA.getName());
}

?