标签:
Javascript是一门解释型的语言,是基于对象的,严格来说并不怎么符合的面向对象的标准,显著的特点就是函数就是“一等对象”,与传统的面向对象语言不同的时,Javascript有“一千种”方法来创建对象,(尽管猿们找个对象的方法基本为零)。《Js高程》为我们讲解了多种创建对象的方法,这些方法之间并没有优劣,只要在具体的环境下选择合适的方法即可。
var mygirl={
name:"js",
age:"24",
shopping:function(){
console.log("give me money!")
}
}
var mygirl=new Object();
mygirl.name="js";
mygirl.age=24;
mygirl.shopping=function(){
console.log("give me money!")
}
var mygirl={};
mygirl.name="js";
mygirl.age=24;
mygirl.shopping=function(){
console.log("give me money!")
}
function createMygirl(name, age)
{
var o = new Object();
o.name=name;
o.age=24;
o.shopping=function()
{
console.log("give me money!")
};
return o;
}
var mygirl=createMygirl("JS",24);
function Girl(name, age)
{
this.name=name;
this.age=24;
this.shopping=function()
{
console.log("give me money!")
};
}
var mygirl=new Girl("JS",24);
var yourgirl=new Girl("HTML",23);
alert(mygirl.shopping == yourgirl.shopping); //false
function Girl(){
}
Girl.prototype.name="JS";
Girl.prototype.age=24;
Girl.prototype.friends=["girl1","girl2"];
Girl.prototype.shopping=function()
{
console.log("走起,闺蜜们"+this.friends);
//叫上闺蜜一起败家
};
var mygirl=new Girl();
mygirl.friends.push("girl3");
mygirl.shopping();//走起,闺蜜们girl1,girl2,girl3
var yourgirl=new Girl();
yourgirl.shopping();//走起,闺蜜们girl1,girl2,girl3
function Girl(name, age)
{
this.name=name;
this.age=24;
this.friends=["girl1","girl2"];
}
Girl.prototype.shopping=function()
{
console.log("走起,闺蜜们"+this.friends);
//叫上闺蜜一起败家
};
var mygirl=new Girl("JS",24);
var yourgirl=new Girl("HTML",25);
mygirl.friends.push("girl3");
console.log(mygirl.friends);//[ ‘girl1‘, ‘girl2‘, ‘girl3‘ ]
console.log(yourgirl.friends);//[ ‘girl1‘, ‘girl2‘ ]
console.log(mygirl.friends===yourgirl.friends);//false
console.log(mygirl.shopping===yourgirl.shopping);//true
function Girl(name, age)
{
this.name=name;
this.age=24;
if (typeof this.sayName!="function") {
Girl.prototype.shopping = function() {
console.log("give me money!")
};
}
}
var mygirl=new Girl("JS",24);
mygirl.shopping();
function Girl(name, age)
{
var o = new Object();
o.name=name;
o.age=age;
o.shopping=function()
{
console.log("give me money!")
};
return o;
}
var mygirl=new Girl("kevin",24);
function SpecialArray(){
//创建数组
var values = new Array();
//添加值
values.push.apply(values, arguments);
//添加方法
values.toPipedString = function(){
return this.join("|");
};
//返回数组
return values;
}
var colors = new SpecialArray("red", "blue", "green")
alert(colors.toPipedString()); //"red|blue|green"
function Girl(name, age,bestfriend)
{
//可以在这里定义私有变量和函数
var o = new Object();
o.name=name;
o.age=age;
o.bestfriend=["girl1","girl2"];
o.shopping=function()
{
console.log("走起,闺蜜们"+this.friends);
//叫上闺蜜一起败家
};
return o;
}
var mygirl=Girl("kevin",24);
标签:
原文地址:http://www.cnblogs.com/star91/p/5554214.html