javascript summary11-12(2)

[b]Enumerating Properties[/b]
The for/in loop discussed in Chapter 6 provides a way to loop through, or enumerate, the properties of an object. This can be useful when debugging scripts or when working with objects that may have arbitrary properties whose names you do not know in advance. The following code shows a function you can use to list the property names of an object:

function DisplayPropertyNames(obj) {

var names = "";

for(var name in obj) names += name + "\n";

alert(names);

}

Note that the for/in loop does not enumerate properties in any specific order, and although it enumerates all user-defined properties, it does not enumerate certain predefined properties or methods.

[b]Undefined Properties[/b]
If you attempt to read the value of a property that does not exist (in other words, a property that has never had a value assigned to it), you end up retrieving the undefined value (introduced in Chapter 3).

You can use the delete operator to delete a property of an object:

delete book.chapter2;
Note that deleting a property does not merely set the property to undefined; it actually removes the property from the object. The for/in loop demonstrates this difference: it enumerates properties that have been set to the undefined value, but it does not enumerate deleted properties.

[b]Instance Methods and Class Methods[/b]

/*

* Complex.js:

* This file defines a Complex class to represent complex numbers.

* Recall that a complex number is the sum of a real number and an

* imaginary number and that the imaginary number i is the

* square root of -1.

*/


/*

* The first step in defining a class is defining the constructor

* function of the class. This constructor should initialize any

* instance properties of the object. These are the essential

* "state variables" that make each instance of the class different.

*/

function Complex(real, imaginary) {

this.x = real; // The real part of the number

this.y = imaginary; // The imaginary part of the number

}


/*
* The second step in defining a class is defining its instance
* methods (and possibly other properties) in the prototype object
* of the constructor. Any properties defined in this object will
* be inherited by all instances of the class. Note that instance
* methods operate implicitly on the this keyword. For many methods,
* no other arguments are needed.
*/


Complex.prototype.magnitude = function( ) {

return Math.sqrt(this.x*this.x + this.y*this.y);

};


/*
* The third step in defining a class is to define class methods,
* constants, and any needed class properties as properties of the
* constructor function itself (instead of as properties of the
* prototype object of the constructor). Note that class methods
* do not use the this keyword: they operate only on their arguments.
*/


// Add two complex numbers and return the result.

Complex.add = function (a, b) {

return new Complex(a.x + b.x, a.y + b.y);

};


// Here are some useful predefined complex numbers.

// They are defined as class properties, where they can be used as

// "constants." (Note, though, that they are not actually read-only.)

Complex.zero = new Complex(0,0);

Complex.one = new Complex(1,0);

Complex.i = new Complex(0,1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值