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());