悟透JavaScript
对象素描
已经说了许多了许多话题了,但有一个很基本的问题我们忘了讨论,那就是:怎样建立对象?
在前面的示例中,我们已经涉及到了对象的建立了。我们使用了一种被称为JavaScript Object Notation(缩写JSON)的形式,翻译为中文就是“JavaScript对象表示法”。
JSON为创建对象提供了非常简单的方法。例如,
创建一个没有任何属性的对象:
var o = {};
创建一个对象并设置属性及初始值:
var person = {name: “Angel“, age: 18, married: false};
创建一个对象并设置属性和方法:
var speaker = {text: “Hello World“, say: function(){alert(this.text)}};
创建一个更复杂的对象,嵌套其他对象和对象数组等:
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);}
};
{
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);}
};
JSON的形式就是用大括“{}”号包括起来的项目列表,每一个项目间并用逗号“,”分隔,而项目就是用冒号“:”分隔的属性名和属性值。这是典型的字典表示形式,也再次表明了 JavaScript里的对象就是字典结构。不管多么复杂的对象,都可以被一句JSON代码来创建并赋值。
其实,JSON就是JavaScript对象最好的序列化形式,它比XML更简洁也更省空间。对象可以作为一个JSON形式的字符串,在网络间自由传递和交换信息。而当需要将这个JSON字符串变成一个JavaScript对象时,只需要使用eval函数这个强大的数码转换引擎,就立即能得到一个JavaScript内存对象。正是由于JSON的这种简单朴素的天生丽质,才使得她在AJAX舞台上成为璀璨夺目的明星。
JavaScript就是这样,把面向对象那些看似复杂的东西,用及其简洁的形式表达出来。卸下对象浮华的浓妆,还对象一个眉目清晰!
I agree with most of this article
The FUN comes the FIRST…
Thank you for every word that you wrote..every one of them touches my heart…
Wow, this blog really caught my eye!
Good point on the concience
I am especially impressed with your post.
I complitely agree with you..Thank you for bringing it up!
wow powerful!
Ooh!! I must remember this one!!
That’s a good question.
I was thinking today that being grateful every moment is great freedom.
Great, Good Luck!
they are … powerful
thank you for agreeing to share a precious moment of your life.
It is fascinating to me how this article about.