这个月工作比较忙,再加上读了一本<javascript权威指南>,还有....所以没有心上来写blog,这个月最后一天了,赶上来补下作业,这个月主要研究了下ajax,其实对于ajax,年初兴起的时候玩过一阵,也做过一些东东,当时的技术也处在异步调用,返回xml,更新网页控件这个阶段,半年过去了,Prototype.js,DWR,TrimPath JSTemplate等技术的成熟,使ajax更平民化,使我们更容易在自已的系统中使用.以下总结一下这个月学到的东东,同时与以前使用ajax进行对比.
高级javascript总结
1.函数也是对象
var
myDog
=
{
bark:
function
()
{
alert('Woof
!
');
}
}
;
var
myCat
=
{
meow:
function
()
{
alert('I am a lazy cat. I will not meow
for
you.');
}
}
;
function
annoyThePet(petFunction)
{
//
let's see what the pet can do
petFunction();
}
//
annoy the dog:
annoyThePet(myDog.bark);
//
annoy the cat:
annoyThePet(myCat.meow);
2.数组,对象成员
js中创建对象以及给对象动态创建成员
var obj = {}; //new, empty object
obj['member_1'] = 'this is the member value';
obj['flag_2'] = false;
obj['some_function'] = function(){ /**//* do something */};
以上代码等同
var obj = {
member_1:'this is the member value',
flag_2: false,
some_function: function(){ /**//* do something */}
};
obj.some_function();
obj['some_function']();
上面代码表示在对象中使用成员,类似于hash表中使用一个key.
3.在js中使用class
//defining a new class called Pet
var Pet = function(petName, age){
this.name = petName;
this.age = age;
};
//let's create an object of the Pet class
var famousDog = new Pet('Santa\'s Little Helper', 15);
alert('This pet is called ' + famousDog.name);
JS中类的继承一般通过
prototype属性
Pet.prototype.communicate = function(){
alert('I do not know what I should say, but my name is ' + this.name);
};
用Prototype.js会简单一些,prototype.js这个框架的代码很值得一读,里面用到了很多高级的JS技巧,巧妙的设计的许多优秀的设计方案.
var Pet = Class.create();
Pet.prototype = {
//our 'constructor'
initialize: function(petName, age){
this.name = petName;
this.age = age;
},
communicate: function(){
alert('I do not know what I should say, but my name is ' + this.name);
}
};
4.将函数作为参数,类似于ruby的闭包写法
var myArray = ['first', 'second', 'third'];
myArray.each( function(item, index){
alert('The item in the position #' + index + ' is:' + item);
} );