JS的面向对象学习

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学习笔记:JS的面向对象</title>
</head>
<body>
<script type="text/javascript">
//构造函数
var testClass = function(name) {
	this.name = name; //成员属性
};

//成员方法
testClass.prototype = {
	init : function() {
		alert(123);
		alert(this.name);
		return this;
	},
	add : function() {
		alert(this.name);
		return this;
	}
};

//实例化对象,也是JS类的继承方法
var obj = new testClass("Javascript");//除非实例化对象,不然在JS里分配大量的new变量地址是一项很慢的操作

//obj.init().add(); //链式调用

obj.name = "Python";   //重新定义成员属性
obj.add = function() { //重新定义成员函数
	alert("new add()");
	return this;
};
obj.next = function() { //定义新的成员函数
	alert("新方法:next()");
	return this;
};
//obj.init().add().next(); //链式调用
//alert(obj.remove);

//另一种继承方式
function subClass(){
    
    var Sex = "男";       // 私有静态属性    
    function checkSex() { //私有静态方法
        return (Sex == "男");
    }
	this.name = "Ruby";
	this.version = "V1.0";
	this.getVersion = function() { //私有方法
		alert(this.version);
		return this;
	};
}

subClass.prototype = new testClass();
subClass.prototype.add = function() { //公共方法
	alert("subClass.add() 新方法,"+this.name+","+this.version);
	return this;
};

//公共静态方法
subClass.pop = function() {
	alert("公共静态方法:subClass.pop()");
}

var obj2 = new subClass();
//obj2.add().getVersion();
//subClass.pop();  //调用公共静态方法

//另一种继承方式
var _class = function() {
	this.add = function() {
		alert("_class.add()");
		return this;
	};
};

var _class2 = function() {
	_class.call(this); //_class2继承_class
	this.remove = function() {
		alert("_class2.remove()");
		return this;
	};
}

var test = new _class2();
//test.add().remove();

//重载的实现方法
function overload(a,b) {
	if(b) overload1(a);
	else overload2(a, b);
}

function overload1(a) {
	alert(a);
}

function overload2(a,b) {
	alert(a+","+b);
}

//接口类的实现
//这部分转自:http://developer.51cto.com/art/200901/104775.htm
function Interface(name, methods) 
{
    if(arguments.length != 2) {
        throw new Error("接口构造函数含" + arguments.length + "个参数, 但需要2个参数.");
    }
    this.name = name;
    this.methods = [];
    if(methods.length < 1) {
        throw new Error("第二个参数为空数组.");
    }
    for(var i = 0, len = methods.length; i < len; i++) {
        if(typeof methods[i][0] !== 'string') {
            throw new Error("接口构造函数第一个参数必须为字符串类型.");
        }
        if(methods[i][1] && typeof methods[i][1] !== 'number') {
            throw new Error("接口构造函数第二个参数必须为整数类型.");
        }
        if(methods[i].length == 1) {
            methods[i][1] = 0;
        } 

        this.methods.push(methods[i]);
    }    
}; 

Interface.registerImplements = function(object) { 

    if(arguments.length < 2) {
        throw new Error("接口的实现必须包含至少2个参数.");
    } 

    for(var i = 1, len = arguments.length; i < len; i++) {
        var interface = arguments[i];
        if(interface.constructor !== Interface) {
            throw new Error("从第2个以上的参数必须为接口实例.");
        }
        for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
            var method = interface.methods[j][0];
            if(!object[method] || typeof object[method] !== 'function' || object[method].getParameters().length != interface.methods[j][1]) {
                throw new Error("接口的实现对象不能执行" + interface.name + "的接口方法" + method + ",因为它找不到或者不匹配.");
            }
        }
    }
}; 

Function.prototype.getParameters = function() { 

    var str = this.toString();
    var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/\s*/g,'');     //取得参数字符串
    try
    {
        return (paramString.length == 0 ? [] : paramString.split(','));
    }
    catch(err)
    {
        throw new Error("函数不合法!");
    }
} 

//使用方法
var Person = new Interface("Person", [["getName", 0], ["setName", 1]]);

function Man() 
{
    this.name = "";
    Interface.registerImplements(this, Person); //它是用来将实例化的this对象继承于Person接口,然后继承类对接口的方法进行实现。
}

Man.prototype.getName = function() {
    return this.name;
};

Man.prototype.setName = function(name) {
    this.name = name;
}; 

var man = new Man();
man.setName("Leepy");
alert(man.getName());

//接口类的继承(不必严格定义实现接口的方法了)
function SchoolBoy(classNo, post)
{
    Man.call(this); //将Man中的关键字this赋值于SchoolBoy对象中去,那么SchoolBoy就拥有了Man构造函数中的name属性了
    this._chassNo = classNo;
    this._post = post;
}

SchoolBoy.prototype = new Man();

SchoolBoy.prototype.getName = function() {
    return "Mr " + this.name;
}

SchoolBoy.prototype.setName = function(name) {
    this.name = name + "'s";
}

var schoolboy = new SchoolBoy("三年二班", "班长");
schoolboy.setName("周杰伦");
alert(schoolboy.getName());

//JSON格式的JS类
var jQuery = {
	add : function() {
	},
	remove : function() {
	}
};

//重新定义JS对象
String.prototype.add = function(str) {
	alert(this+str);
}

//"PHP".add(" & MySQL");
</script>    
</body>
</html>

你可能感兴趣的:(JS的面向对象学习)