前段时间,在Team里面做的一个关于JavaScript和OOP的topic,终于记得带过来,share出来!
1. Data Structure
JavaScript中的数据很简洁的。简单数据只有 undefined, null, boolean, number和string这五种,而复杂数据只有一种,即object. 这就好比中国古典的朴素唯物思想,把世界最基本的元素归为金木水火土,其他复杂的物质都是由这五种基本元素组成。
JavaScript 中的代码只体现为一种形式,就是
function.
任何一个JavaScript的标识、常量、变量和参数都只是unfined, null, boolen, number, string, object 和 function类型中的一种,也就typeof返回值表明的类型。除此之外没有其他类型了。
2. Object. 所谓“对象化”,就是可以将数据和代码组织成复杂结构的能力。JavaScript中只有object类型和function类型提供了对象化的能力。
所谓“函数的对象化能力”,是指任何一个函数都可以为其动态地添加或去除属性,这些属性可以是简单类型,可以是对象,也可以是其他函数。
也就是说,函数具有对象的全部特征,你完全可以把函数当对象来用。其实,函数就是对象,只不过比一般的对象多了一个括号“()”操作符,这个操作符用来执行函数的逻辑。
JavaScript使用了一种被称为JavaScript Object Notation(缩写JSON)的形式,翻译为中文就是“JavaScript对象表示法”。
var company =
{
name: "Microsoft",
product: "softwares",
chairman: {name: "Bill Gates", age: 53, Married: true},
employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],
readme: function() {document.write(this.name + " product " + this.product);}
};
3. OOP
1 )Encapsulation.
var Company = function(name,product){
var temp=”temp”;
this.name= name;
this.product= product;
this.chairman= {name: "Bill Gates", age: 53, Married: true};
this.employees= [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}];
this.readme= function() {document.write(this.name + " product " + this.product);};
};
var company = new Company(“Microsoft”,”Inventory”);
2) Inherit
Prototype: prototype源自法语,软件界的标准翻译为“原型”,代表事物的初始形态,也含有模型和样板的意义。JavaScript的所有function类型的对象都有一个prototype属性。这个prototype属性本身又是一个object类型的对象,因此我们也可以给这个prototype对象添加任意的属性和方法。既然prototype是对象的“原型”,那么由该函数构造出来的对象应该都会具有这个“原型”的特性。
function Person(name) //基类构造函数
{
this.name = name;
};
Person.prototype.SayHello = function() //给基类构造函数的prototype添加方法
{
alert("Hello, I'm " + this.name);
};
function Employee(name, salary) //子类构造函数
{
Person.call(this, name); //调用基类构造函数
this.salary = salary;
};
Employee.prototype = new Person(); //建一个基类的对象作为子类原型的原型
Employee.prototype.ShowMeTheMoney = function() //给子类添构造函数的prototype添加方法
{
alert(this.name + " $" + this.salary);
};
var BillGates = new Person("Bill Gates"); //创建基类Person的BillGates对象
var SteveJobs = new Employee("Steve Jobs", 1234); //创建子类Employee的SteveJobs对象
BillGates.SayHello(); //通过对象直接调用到prototype的方法
SteveJobs.SayHello(); //通过子类对象直接调用基类prototype的方法,关注!
SteveJobs.ShowMeTheMoney(); //通过子类对象直接调用子类prototype的方法
3)
Polymorphism
function Shape(margin)
{
this.margin =margin;
this.size=function(){document.write("I am Original. my margin is:"+this.margin+'<br>');};
this.hello=function(){document.write("My margin is:"+this.margin+'<br>');}
};
function Circle(margin)
{
Shape.call(this,margin);
this.size = function(){document.write("I am Circle! my size is :"+3.14*this.margin*this.margin+'<br>');}
}
Circle.prototype=new Shape();
function Rectangle(margin)
{
Shape.call(this,margin);
this.size = function(){document.write("I am rectangle! my size is: "+this.margin*this.margin+'<br>');}
}
Rectangle.prototype = new Shape();
var shape = new Shape(100);
shape.size();
var circle = new Circle(100);
circle.size();
circle.hello();
var rect = new Rectangle(100);
rect.size();
rect.hello();
1. Data Structure
JavaScript中的数据很简洁的。简单数据只有 undefined, null, boolean, number和string这五种,而复杂数据只有一种,即object. 这就好比中国古典的朴素唯物思想,把世界最基本的元素归为金木水火土,其他复杂的物质都是由这五种基本元素组成。
JavaScript
任何一个JavaScript的标识、常量、变量和参数都只是unfined, null, boolen, number, string, object 和 function类型中的一种,也就typeof返回值表明的类型。除此之外没有其他类型了。
2. Object.
所谓“函数的对象化能力”,是指任何一个函数都可以为其动态地添加或去除属性,这些属性可以是简单类型,可以是对象,也可以是其他函数。
也就是说,函数具有对象的全部特征,你完全可以把函数当对象来用。其实,函数就是对象,只不过比一般的对象多了一个括号“()”操作符,这个操作符用来执行函数的逻辑。
JavaScript使用了一种被称为JavaScript Object Notation(缩写JSON)的形式,翻译为中文就是“JavaScript对象表示法”。
var company =
{
name: "Microsoft",
product: "softwares",
chairman: {name: "Bill Gates", age: 53, Married: true},
employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],
readme: function() {document.write(this.name + " product " + this.product);}
};
3. OOP
var Company = function(name,product){
var temp=”temp”;
this.name= name;
this.product= product;
this.chairman= {name: "Bill Gates", age: 53, Married: true};
this.employees= [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}];
this.readme= function() {document.write(this.name + " product " + this.product);};
};
var company = new Company(“Microsoft”,”Inventory”);
function Person(name) //基类构造函数
{
this.name = name;
};
Person.prototype.SayHello = function() //给基类构造函数的prototype添加方法
{
alert("Hello, I'm " + this.name);
};
function Employee(name, salary) //子类构造函数
{
Person.call(this, name); //调用基类构造函数
this.salary = salary;
};
Employee.prototype = new Person(); //建一个基类的对象作为子类原型的原型
Employee.prototype.ShowMeTheMoney = function() //给子类添构造函数的prototype添加方法
{
alert(this.name + " $" + this.salary);
};
var BillGates = new Person("Bill Gates"); //创建基类Person的BillGates对象
var SteveJobs = new Employee("Steve Jobs", 1234); //创建子类Employee的SteveJobs对象
BillGates.SayHello(); //通过对象直接调用到prototype的方法
SteveJobs.SayHello(); //通过子类对象直接调用基类prototype的方法,关注!
SteveJobs.ShowMeTheMoney(); //通过子类对象直接调用子类prototype的方法
{
this.margin =margin;
this.size=function(){document.write("I am Original. my margin is:"+this.margin+'<br>');};
this.hello=function(){document.write("My margin is:"+this.margin+'<br>');}
};
function Circle(margin)
{
Shape.call(this,margin);
this.size = function(){document.write("I am Circle! my size is :"+3.14*this.margin*this.margin+'<br>');}
}
Circle.prototype=new Shape();
function Rectangle(margin)
{
Shape.call(this,margin);
this.size = function(){document.write("I am rectangle! my size is: "+this.margin*this.margin+'<br>');}
}
Rectangle.prototype = new Shape();
var shape = new Shape(100);
shape.size();
var circle = new Circle(100);
circle.size();
circle.hello();
var rect = new Rectangle(100);
rect.size();
rect.hello();