javascript面向对象 代码详解(四)

function Box()
{}

var box = new Box();
alert(box.prototype);   //使用对象实例无法访问到prototype
alert(box.__proto__);    //使用对象实例访问prototype的指针
alert(Box.prototype);   //使用构造函数名(对象名)访问prototype


function Box()
{}

//使用字面量的方式创建原型对象,这里面的{}就是对象,是Object
//new Object  就相当于{}
Box.prototype = 
{
	name : "Lee",
	age : 100,
	run : function()
	{
		return this.name + this.age + "运行中";
	}
}

var box = new Box();
alert(box.name);
alert(box.run());

//构造方法创建原型对象的方式
function Box()
{}

Box.prototype.name = "Lee";
Box.prototype.age = 100;
Box.prototype.run = function()
{
	return this.name + this.age + "运行中...";
}


var box = new Box();
alert(box.constructor);
//打印
//function Box(){}

//字面量创建原型对象的方式
function Box()
{}

Box.prototype = 
{
	name : "Lee",
	age : 100,
	run : function()
	{
		return this.name + this.age + "运行中...";
	}
}
var box = new Box();
alert(box.constructor);
//打印出function Object{[native code]}
//false
alert(box.constructor == Box);
//true
alert(box.constructor == Object);

//构造方法创建原型对象的方式
function Box()
{}

Box.prototype.name = "Lee";
Box.prototype.age = 100;
Box.prototype.run = function()
{
	return this.name + this.age + "运行中...";
}


var box = new Box();
alert(box.constructor);
//打印
//function Box(){}
//打印true
alert(box.constructor == Box)


//字面量创建原型对象的方式
function Box()
{}

Box.prototype = 
{
	//强制指向Box
	constructor : Box,
	name : "Lee",
	age : 100,
	run : function()
	{
		return this.name + this.age + "运行中...";
	}
}
var box = new Box();
alert(box.constructor);

//打印true
alert(box.constructor == Box);

//字面量创建原型对象的方式
function Box()
{}

Box.prototype = 
{
	//强制指向Box
	constructor : Box,
	name : "Lee",
	age : 100,
	run : function()
	{
		return this.name + this.age + "运行中...";
	}
}

//重写原型对象
Box.prototype = 
{
	//这里不会保留之前原型的任何信息
	//把原来的原型对象和构造函数对象的关系给切断了
	age :200
};

var box = new Box();
//打印undefined
alert(box.name);

var box = [5,1,5,6,7,2,4];
//数组排序
alert(box.sort());

//查看sort是否是Array原型对象里的方法
alert(Array.prototype.sort);

alert(String.prototype.substring);

//扩展String类型的方法
String.prototype.addstring = function()
{
	//this就是被传递进来的字符串
	return this + ",被添加了"
}

alert("Lee".addstring());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值