JS中创建对象的两种方法
来源:互联网 发布:linux系统运维手册 编辑:程序博客网 时间:2024/06/05 21:55
<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();//使用方法来模拟classfunction 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>