JavaScript Objects
http://www.codeproject.com/Articles/637892/JavaScript-Objects在 javascript 中有很多方式来创建对象,所以创建对象的方式使用起来非常灵活。那么,到底哪一种方式是最恰当的对象创建方式呢?构造模式,原型模式还是对象原意模式(Object literal)呢?
但这些模式具体是怎么回事呢?
在开始讲解之前,让我们先清楚地介绍一下关于 javascript 基本知识。
有没有可能在 javascript 中实现面向对象编程的方式呢?
答案是可能的,javascript 是可以创建对象的!这种对象可以包含数据及能够操作数据的方法,甚至可以包含其他对象。它没有类但拥有构造函数;它没有类继承机制,但是可以通过原型(prototype)实现继承。
现在看起来,我们已经了解了在 javascript 中创建对象及实现基于对象编程时所必须的组成部分。
我们都知道 javascript 拥有私有变量。一个通过“var”关键字定义的变量,只能在函数体中被访问,而不能在函数外被访问。那么,如果我们不通过使用“var”关键字来定义变量会怎样呢?我们现在不对这个问题进行深入探讨,可能是通过“this”进行访问的,我会在另外的时间来详细讲述这个问题。
现在回到之前的问题。到底哪一种方式是最恰当的对象创建方式呢?
让我们用已经知晓的知识,通过创建Person的对象是来试验一下。
var Person = { firstName : 'John', lastName : 'Cody', fullName : '', message : '', createFullName : function () { fullName = this.firstName + ' ' + this.lastName; }, changeMessage : function (msg) { this.message = msg; }, getMessage : function () { this.createFullName(); return this.message + ' ' + fullName; } } Person.firstName = 'Eli'; Person.lastName = 'Flowers' Person.changeMessage('welcome'); var message = Person.getMessage(); // welcome Eli Flowers alert(message);
var Person = function() { this.firstName = 'John'; this.lastName = 'Cody'; var fullName = ''; this.message = ''; var _that = this; var createFullName = function () { fullName = _that.firstName + ' ' + _that.lastName; } this.changeMessage = function (msg) { this.message = msg; } this.getMessage = function () { createFullName(); return this.message + ' ' + fullName; } } var person1 = new Person(); person1.firstName = 'Eli'; person1.lastName = 'Flowers' person1.changeMessage('welcome'); var message = person1.getMessage(); // welcome Eli Flowers alert(message);
var Person = function () { //private var firstName = 'John'; var lastName = 'Cody'; var fullName = ''; var message = ''; var createFullName = function () { fullName = firstName + ' ' + lastName; } //public setters var setMessage = function (msg) { message = msg; } var setFirstName = function (fName) { firstName = fName; } var setLastName = function (lName) { lastName = lName; } var getMessage = function () { createFullName(); return message + ' ' + fullName; } //functions exposed public return { setFirstName: setFirstName, setLastName: setLastName, setMessage: setMessage, getMessage: getMessage }; }; var person1 = new Person(); person1.setFirstName('Eli'); person1.setLastName('Flowers'); person1.setMessage('welcome'); var message = person1.getMessage(); // welcome Eli Flowers alert(message);
var Person = function () { //private var welcomeMessage = 'welcome'; var fullName = ''; var firstName = ''; var lastName = ""; var createFullName = function () { Person.prototype.setFirstName('asdsad'); fullName = firstName + ' ' + lastName; }; //constructor var Person = function () { }; //will be created evrytime //public Person.prototype = { getFullName: function () { createFullName(); return welcomeMessage + ' ' + fullName; }, setFirstName: function (fName) { firstName = fName; }, setLastName: function (lName) { lastName = lName; }, ChangeMessage: function (mesg) { welcomeMessage = mesg; } } return new Person(); // Person; //new Person(); }; var person1 = new Person(); person1.setFirstName ('Eli'); person1.setLastName('Flowers'); person1.ChangeMessage('welcome'); var message = person1.getFullName(); // welcome asdsad Flowers alert(message);
(function () { var Person = function () { this._fullName = ''; this.welcomeMessage = ''; this.firstName = ''; this.lastName = ""; _that = this; this._createFullName = function () { this.ChangeMessage('Namaste'); this._fullName = this.firstName + ' ' + this.lastName; }; } //Shared Functions for Code optimization Person.prototype = { constructor: Person, getFullName: function () { this._createFullName(); return this.welcomeMessage + ' ' + this._fullName; }, ChangeMessage: function (mesg) { this.welcomeMessage = mesg; } } this.Person = Person; })(); var person1 = new Person(); person1.firstName = 'Eli'; person1.lastName = 'Flowers'; person1.ChangeMessage('Welcome'); var message = person1.getFullName(); // Namaste Eli Flowers alert(message);
经常使用 javascript,对于它的印象一直都是直接拷贝过来就可以用的。最近使用 extjs,它的类框架非常好用。从这样文章也明白在 javascript 中实现类的各种方式,以及在文章最后讨论了类中私有成员的实现情况。