Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273745
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: JavaScript

2013-12-24 14:48:23

//面向对象
//工厂函数
function createObject(name,age){
var obj = new Object();
obj.name=name;
obj.age=age;
obj.run=function(){
return this.name+this.age+'运行中..';
};
return obj;
};


var box1=createObject('Lee',100);
var box2=createObject('jack',200);


alert(box1.run());
alert(box2.run());


//构造函数创建对象
function Box(name,age){
this.name=name;
this.age=age;
this.run = function(){
return this.name + this.age +'运行中..';
};
};
//1.构造函数没有new Object,但它后台自动var obj=new Object
//2.this相当于obj
//3.构造函数不需要返回对象引用,他是后台自动返回的


//1.构造函数也是函数,但是第一个字母必须大写
//2.必须new构造函数名(),new Box(),这个Box第一个字母也是大写的
//3.必须使用new运算符
var box1=new Box('Lee',100);
var box2=new Box('jack',200);


alert(box1.run());
alert(box2.run());

阅读(740) | 评论(0) | 转发(0) |
0

上一篇:javacript07-内置对象

下一篇:javascript09-原型

给主人留下些什么吧!~~