原文 Quick guide to somewhat advanced JavaScript tour of some OO features
出处 
http://www.sergiopereira.com/articles/advjs.html什么是JSON? JavaScript Object Notation -------javascript对象符号,一个Ajax中砰然出现的新话题.其实它就是javascript中申明对象的方法。让我们来看一个示例

 var myPet =
var myPet = 
{ 
   color: 'black', 
   leg_count: 4, 
   communicate: function(repeatCount){ 
 for(i=0;i<repeatCount;i++) alert('Woof!');}
for(i=0;i<repeatCount;i++) alert('Woof!');} 
};

这里我们就创建了一个对象引用,这个对象包含了2个属性(color,leg_count)和一个方法(communicate),
在myPet对象的列表中不难看出,这些属性和方法之间都是用逗号","分隔开来的。属性很好理解,而方法的创建则是由一个匿名function标记的。
在该对象被创建以后并且以myPet变量声明了以后我们就可以这样使用了:
 alert('my pet is ' + myPet.color);
alert('my pet is ' + myPet.color);
 alert('my pet has ' + myPet.legCount + ' legs');
alert('my pet has ' + myPet.legCount + ' legs');
 //if you are a dog, bark three times:
//if you are a dog, bark three times:
 myPet.communicate(3);
myPet.communicate(3);

大家可以看到,JSON可以漂亮地应用到javascript中任何一个地方,作为function参数、返回值、服务器响应...
你理解了吗?function同样也是一个对象。
---------------------------------------------------------------
或许JS程序员中有些人从来就不这么认为,但是function确实也是一个Object。下面我们可以做个实验来验证一下,将你的function给你的另一个function做为参数传递,就传一个String一样。

 var myDog = {
var myDog = {

 bark: function()
    bark: function() {
{
 alert('Woof!');
        alert('Woof!');
 }
    }
 };
};


 var myCat =
var myCat =  {
{

 meow: function()
    meow: function() {
{
 alert('I am a lazy cat. I will not meow for you.');
        alert('I am a lazy cat. I will not meow for you.');
 }
    }
 };
};
 
 

 function annoyThePet(petFunction)
function annoyThePet(petFunction) {
{
 //let's see what the pet can do
    //let's see what the pet can do
 petFunction();
    petFunction();
 }
}

 //annoy the dog:
//annoy the dog:
 annoyThePet(myDog.bark);
annoyThePet(myDog.bark);
 //annoy the cat:
//annoy the cat:
 annoyThePet(myCat.meow);
annoyThePet(myCat.meow);

在这里我们传递了myDog.bark和myCat.meow给annoyThePet。注意它们后面都没有带括号"()",如果不这样做的话我们传递的将不是"方法",而是该方法的返回值(在此例中为"Undefine")。
如果你想我的懒猫学狗狗叫,你可以这样做:
 myCat.meow = myDog.bark;
myCat.meow = myDog.bark;
 myCat.meow(); //alerts 'Woof!'
myCat.meow(); //alerts 'Woof!'

 
Arrays, items 和对象成员object members
请看下面2行

 var a = new Array();
var a = new Array();
 var b = [];
var b = [];

你可以在array中添加items:
 var a = ['first', 'second', 'third'];
var a = ['first', 'second', 'third'];
 var v1 = a[0];
var v1 = a[0];
 var v2 = a[1];
var v2 = a[1];
 var v3 = a[2];
var v3 = a[2];

但是你在声明时对索引不做数量限制,你将可以通过它的名字访问这个JS对象中的任何成员,就象这样,创建一个空的对象,然后就可以不停地添加了:

 var obj =
var obj =  {}; //new, empty object
{}; //new, empty object
 obj['member_1'] = 'this is the member value';
obj['member_1'] = 'this is the member value';
 obj['flag_2'] = false;
obj['flag_2'] = false;

 obj['some_function'] = function()
obj['some_function'] = function() { /**//* do something */};
{ /**//* do something */};
 
            等价于:

 var obj =
var obj =  {
{
 member_1:'this is the member value',
    member_1:'this is the member value',
 flag_2: false,
    flag_2: false,

 some_function: function()
    some_function: function() { /**//* do something */}
{ /**//* do something */}
 };
};

在JS中,对象和关系型数组(hashes)两个概念通常是没有区别,下面的这两行代码是等价的:

 obj.some_function();
obj.some_function();
 obj['some_function']();
obj['some_function']();

等了这么久,我可以拥有自己的Class了么?
OO的强大力量来自于类Class。当然了我们的JavaScript同样也可以做到这一点,请看吧:
 //defining a new class called Pet
//defining a new class called Pet

 var Pet = function(petName, age)
var Pet = function(petName, age) {
{
 this.name = petName;
    this.name = petName;
 this.age = age;
    this.age = age;
 };
};

 //let's create an object of the Pet class
//let's create an object of the Pet class
 var famousDog = new Pet('Santa\'s Little Helper', 15);
var famousDog = new Pet('Santa\'s Little Helper', 15);
 alert('This pet is called ' + famousDog.name);
alert('This pet is called ' + famousDog.name);

但是我们如何能够将方法加到我们的Pet  Class中来呢。我们可以为我们所有的类定义一个prototype属性。prototype属性其实也是一个对象,它包含一个类所拥有的一切其他对象。甚至默认JS类(Date,String,Number)也可以拥有prototype对象,使得各种新的方法和属性能扩充其中,让基础类获得更多的特性。

 Pet.prototype.communicate = function()
Pet.prototype.communicate = function() {
{ 
 alert('I do not know what I should say, but my name is ' + this.name);
    alert('I do not know what I should say, but my name is ' + this.name);
 };
};

别忘了,我们得先导入 prototype.js才行。
ps: 个人认为象下面创建类,可能更简洁一点。
 var Pet = Class.create();
var Pet = Class.create();

 Pet.prototype =
Pet.prototype =  {
{
 //our 'constructor'
    //our 'constructor'

 initialize: function(petName, age)
    initialize: function(petName, age) {
{
 this.name = petName;
        this.name = petName;
 this.age = age;
        this.age = age;
 },
    },
 
    

 communicate: function()
    communicate: function() {
{
 alert('I do not know what I should say, but my name is ' + this.name);
        alert('I do not know what I should say, but my name is ' + this.name);
 }
    }
 };
};    

如果你没有使用过支持closure的语言,象Ruby,C#,你会发现象这样的语法真是太有趣了:
 var myArray = ['first', 'second', 'third'];
var myArray = ['first', 'second', 'third'];

 myArray.each( function(item, index)
myArray.each( function(item, index) {
{
 alert('The item in the position #' + index + ' is:' + item);
    alert('The item in the position #' + index + ' is:' + item);
 });
});

未完带续.........