javascript 面向对象的程序设计
2012-09-20 23:07
375 查看
1工厂模式
用函数封装创建对象的细节
2:构造函数mos
或者
3:原型模式:
prototype(原型),这个属性是一个对象,他的用途是包含可以由特定类型的所有实例共享的属相和方法。
组合构造函数和原型模式
用函数封装创建对象的细节
function createPerson(name, age, job){ var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function(){ alert(this.name); } }
2:构造函数mos
function Person(name, age , job){ this.name = name; this.age = age; this.job = job; this.sayName = function(){ alert(this.name); } }
或者
function Person(name, age , job){ this.name = name; this.age = age; this.job = job; this.sayName = sayName; } function sayName(){ alert(this.name); }但是这样破坏了封装性
3:原型模式:
prototype(原型),这个属性是一个对象,他的用途是包含可以由特定类型的所有实例共享的属相和方法。
function Person(){ } Person.prototype = { //constructor:Person, name:"wang"; age:23; sayName:function(){ alert(this.name); } };
组合构造函数和原型模式
function Person(name, age , job){ this.name = name; this.age = age; this.job = job; this.friends = ["one", "two"]; } Person.prorotype = { constructor:Person; sayName : function(){ alert(this.name); } }
相关文章推荐
- JavaScript面向对象程序设计(5): 类
- Javascript面向对象程序设计培训回顾
- JavaScript 面向对象程序设计(下)——继承与多态
- JavaScript面向对象程序设计(7): 闭包
- JavaScript 面向对象程序设计(下)——继承与多态(转)
- JavaScript中的面向对象程序设计
- javascript高级程序设计笔记(第6章 面向对象的程序设计)
- JavaScript 面向对象程序设计(上)——封装[转]
- 【JavaScript学习】面向对象的程序设计(7):寄生式继承和寄生组合式继承
- Javascript高级程序设计——14.面向对象与原型(2)
- 你不知道的JavaScript--面向对象高级程序设计
- JavaScript 面向对象程序设计(下)——继承与多态
- JavaScript之理解对象(面向对象的程序设计)
- Javascript高级程序设计第二版第六章--面向对象程序设计(Object-Oriented Programming)简称OOP编程--笔记
- JavaScript之面向对象程序设计
- JavaScript 面向对象程序设计(下)——继承与多态
- javascript 面向对象程序设计--深刻理解对象
- 浅谈JavaScript的面向对象程序设计(三)
- JavaScript面向对象程序设计(3): 对象 推荐
- javascript面向对象程序设计高级特性经典教程(值得收藏)