封装Encapsulation
如下代码,这就算是封装了
(function (windows, undefined) {
var i = 0;//相对外部环境来说,这里的i就算是封装了
})(window, undefined);
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
继承Inheritance
(function (windows, undefined) {
//父类
function Person() { }
Person.prototype.name = "name in Person";
//子类
function Student() { }
Student.prototype = new Person(); //修复原型
Student.prototype.constructor = Student; //构造函数
Student.prototype.supr = Person.prototype; //父类
//创建子类实例
var stu = new Student();
Student.prototype.age = 28;
Student.prototype.supr.name = "name in Student instance";
//打印子类成员及父类成员
alert(stu.name); //name in Student instance
alert(stu.supr.name); //name in Person
alert(stu.age); //28
})(window, undefined);
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
多态Polymorphism
有了继承,多态就好办了
//这就是继承了
(function (windows, undefined) {
//父类
function Person() { }
Person.prototype.name = "name in Person";
Person.prototype.learning = function () {
alert("learning in Person")
}
//子类
function Student() { }
Student.prototype = new Person(); //修复原型
Student.prototype.constructor = Student; //构造函数
Student.prototype.supr = Person.prototype; //父类
Student.prototype.learning = function () {
alert("learning in Student");
}
//工人
function Worker() { }
Worker.prototype = new Person(); //修复原型
Worker.prototype.constructor = Worker; //构造函数
Worker.prototype.supr = Person.prototype; //父类
Worker.prototype.learning = function () {
alert("learning in Worker");
}
//工厂
var personFactory = function (type) {
switch (type) {
case "Worker":
return new Worker();
break;
case "Student":
return new Student();
break;
}
return new Person();
}
//客户端
var person = personFactory("Student");
person.learning(); //learning in Student
person = personFactory("Worker");
person.learning(); //learning in Worker
})(window, undefined);
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }