您的位置:首页 > Web前端 > JavaScript

JS中创建对象的两种方法

2013-09-04 10:16 148 查看
<html>
<head>

 <script type="text/javascript">
//一对{}括起来
var myObj =
    {
        'id': 1,
		'name': 'myObj',
        'fun': function() {
            document.writeln(this.id + '-' + this.name);//以"对象.属性"方式访问
        },
        'fun1': function() {
            document.writeln(this['id'] + '+' + this['name']);//以集合方式访问
        }
    };
    myObj.fun();
    myObj.fun1();
//使用方法来模拟class
function myClass() {  
            this.id = 5;  
            this.name = 'myclass';  
            this.getName = function() {  
                return this.name;  
            }  
        }  
        var my = new myClass();  
        alert(my.id);  
        alert(my.getName());  
</script>
</head>
<body >
  hello
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: