创建ECMAScript对象的三种方法

<script type="text/javascript">

/**
 * 1.使用构造函数创建Javascript对象
 */
function Test(name,url)
{
    this.name='first';
    this.url='www.first.com';
    this.say=function(){alert('my name is '+this.name);}
}
$(function(){
    $('#test').bind('keypress',function(){
        var test=new Test();
        test.say();
        });
})


/**
 * 2.使用定义法直接定义Javascript对象
 */
var Test = {};
Test.name='second';
Test.url='www.second.com';
Test.say=function(){alert('my name is '+this.name);}
$(function(){
    $('#test').bind('keypress',function(){    
    Test.say();
    });
})

/**
 * 3.使用JSON法创建Javascipt对象
 */
var Test={
    name:'third',
    url:'www.third.com',
    say:function(){alert('my name is '+this.name);}
}
$(function(){
    $('#test').bind('keypress',function(){
    Test.say();
        });
})

</script>

posted on 2011-01-24 09:59  十二  阅读(144)  评论(0编辑  收藏  举报

导航