Advanced Object Creation(JS,翻译MSDN文章)

 

Advanced Object Creation

  A constructor is a function you call to instantiate and initialize a particular type of object. You invoke a constructor with the new keyword. Here are a few examples of using constructors.
  
一个构造器是一个函数,你可以调用它实例化和初始化一个特殊的对象类型。你可以使用new关键字调用一个构造器。这里有一些使用构造器的例子:

var myObject = new Object();             // Creates a generic object with no properties.

var myBirthday = new Date(1961, 5, 10);  // Creates a Date object.

var myCar = new Car();                   // Creates a user defined object, and initializes its properties.

 

The constructor is passed a reference to a newly created empty object as the value of the special this keyword. It is then responsible for performing appropriate initialization for the new object (creating properties and giving them initial values). When completed, the constructor returns a reference to the object it constructed.

 

构造器将被传递一个引用到一个新创建的空对象,作为特色的this关键字的值。这时它会负责适当的初始化这个新对象(创建属性和赋予初始值)。当完成的时候,构造器返回一个它构造的对象的引用。

Writing Constructors 写构造器

You can create objects and initialize them using the new operator in conjunction with predefined constructor functions such as Object(), Date(), and Function(). A powerful feature of object-oriented programming is the ability to define custom constructor functions to create custom objects for use in your scripts. You create custom constructors so you can create objects with properties already defined. Here is an example of a custom constructor (note the use of the this keyword).

你可以使用new创建对象并初始化它们,需要使用预先定义的函数,例如objcet(),Data()或者Function().一个强有力的对象导向程序的特质是在脚本它可以自定义构造器去创造自定义对象来使用。你可以创造自定义的构造器,所以你可以使用已经定义的属性创建对象。这里有一个自定义构造器的例子(注意this关键字的使用)

 

function Circle (xPoint, yPoint, radius) {

    this.x = xPoint;  // The x component of the center of the circle.

    this.y = yPoint;  // The y component of the center of the circle.

    this.r = radius;  // The radius of the circle.

}

 

When you invoke the Circle constructor, you supply values for the circle's center point and the radius (these elements are all that is needed to completely define a unique circle object). You end up with a Circle object that contains three properties. Here is how you would instantiate a Circle object.

 

当你调用Circle构造器的时候,你为其提供了中点和半径的值(这些元素是完全定义一个唯一的Circle对象的全部)。这里以创建一个包含3个属性的Circle对象告终。下面告诉你如何实例化一个circle对象:

 

var aCircle = new Circle(5, 11, 99);

 

Using Prototypes to Create Objects 使用prototype来创建对象

When you write a constructor, you can use properties of the prototype object (which is itself a property of every constructor) to create inherited properties, and shared methods. Prototype properties and methods are copied by reference into each object of a class, so they all have the same values. You can change the value of a prototype property in one object, and the new value overrides the default, but only in that one instance. Other objects that are members of the class are not affected by the change. Here is an example that makes use of the custom constructor, Circle (note the use of the this keyword).

 

当你写构造器的时候,你可以使用对象的prototype属性(它是每个构造器都有点属性)来创建内在的属性并且公开方法。Prototype属性和方法被复制到一个类的每个对象。因此他们有同样的值。你可以在一个对象中改变prototype属性的值,新值将覆盖默认的值,但是仅仅只改变这个实例。其他对象所有方法不会受到这个变化的影响。这里有一个关于自定义构造器的例子,circle(注意this关键字的使用)

 

Circle.prototype.pi = Math.PI;

function ACirclesArea () {

    return this.pi * this.r * this.r; // The formula for the area of a circle is Ïr2.

}

Circle.prototype.area = ACirclesArea; // The function that calculates the area of a circle is now a method of the Circle Prototype object.

var a = ACircle.area();     // This is how you would invoke the area function on a Circle object.

 

Using this principle, you can define additional properties for predefined constructor functions (which all have prototype objects). For example, if you want to be able to remove leading and trailing spaces from strings (similar to VBScript's Trim function), you can create your own method on the String prototype object, and all strings in your script will automatically inherit the method.

 

使用这样的原理,你可以定义附加的属性对象预先定义的狗在其函数(所有包含prototype对象).例如,如果你想移除字符串前置和后缀的空格(类似VBScript TRIM函数),你可以创建你自己的方法在stringprototype对象,那么在你的脚本里所有的string将自动实现内在的方法

// Add a function called trim as a method of the prototype

// object of the String constructor.

String.prototype.trim = function()

{

    // Use a regular expression to replace leading and trailing

    // spaces with the empty string

    return this.replace(/(^\s*)|(\s*$)/g, "");

}

 

// A string with spaces in it

var s = "    leading and trailing spaces    ";

 

// Displays "    leading and trailing spaces     (35)"

window.alert(s + " (" + s.length + ")");

 

// Remove the leading and trailing spaces

s = s.trim();

// Displays "leading and trailing spaces (27)"

window.alert(s + " (" + s.length + ")");

posted on 2008-05-05 15:26  仁面寿星  阅读(418)  评论(0编辑  收藏  举报