我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

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 々上善若水々 阅读(209) 评论(0)  编辑  收藏 所属分类: JavaScript


只有注册用户登录后才能发表评论。


网站导航: