我的漫漫程序之旅

专注于JavaWeb开发

JavaScript面向对象基础 (Object Oriented Basics)

<HTML>
 
<HEAD>
  
<TITLE> Object oriented Basics</TITLE>
 
</HEAD>
 
<BODY>
<script>
    
// A simple function which takes a name and saves it to the current context
    function User(name)
    
{
        
this.name = name;
    }

    
//Create a new instance of that function ,with the specified name
    var me = new User("zdw");
    
//We can see that its name has been set as a property of itself
    alert(me.name);
    
//And that it is an instance of the User object
    alert(me.constructor == User);
    
//Now,since User() is just a function,what happens 
    //when we treat it as such?
    User("admin");
    
//Since its 'this' context wasn't set,it defaults to the global 'window'
    //object,meaning that window.name is equal to the name provided
    alert(window.name);
    
//Create a new,simple,Person object
    function Person(name)
    
{
        
this.name = name;
    }

    
//Create a new Person object
    var p1 = new Person("person");
    
//Alsos create a new Person Object(from the constructor reference of the first)
    var p2 = new p1.constructor("zhangsan");
    
//show result
    alert(p2.name);
</script>
 
</BODY>
</HTML>

posted on 2008-07-18 16:45 々上善若水々 阅读(16) 评论(0)  编辑  收藏 所属分类: JavaScript


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 

相关链接:
网站导航:

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

留言簿(12)

随笔档案(24)

文章分类(277)

文章档案(228)

搜索

最新评论

阅读排行榜

评论排行榜