首页 > Web开发 > 详细

js中四种创建对象的方式

时间:2015-11-19 16:30:33      阅读:239      评论:0      收藏:0      [点我收藏+]

一、

1 var user = new Object();
2 user.first="Brad";
3 user.last="Dayley";
4 user.getName = function( ) { return this.first + " " + this.last; }

二、

1 var user = {
2   first: ‘Brad‘,
3   last: ‘Dayley‘,
4   getName: function( ) { return this.first + " " + this.last; }};

 三、

 1 function zch(first, last) {
 2     this.first = first;
 3     this.last = last;
 4     this.getName = function () {
 5         return this.first + " " + this.last;
 6     };
 7 }
 8 
 9 var user = new zch("zheng", "chunhao");
10 console.log(user.getName());

 四、

 1 function User(first, last){
 2     this.first = first;
 3     this.last = last;
 4 }
 5 User.prototype = {
 6     getFullName: function(){
 7         return this.first + " " + this.last;
 8     },
 9     sayHello: function(){
10         console.log(‘hello,zhengchunhao.‘);
11     }
12 };
13 
14 var zch = new User("zheng", "chunhao");
15 console.log(zch.getFullName());
16 
17 zch.sayHello();

 

js中四种创建对象的方式

原文:http://www.cnblogs.com/zhengchunhao/p/4977658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!