自定義js對象javascript
var article=function(title){ this.title = title; var arrys=new Array(); this.addPerson=function(person){ var i=arrys.length; arrys[i]=person; } this.showPerson=function(){ var persons=""; for(var i=0;i<arrys.length;i++){ persons+=arrys[i]; } return persons; } } window.onload=function(){ var news=new article("nihao");//建立一個對象 news.addPerson("kebe"); news.addPerson("jardan"); news.addPerson("james"); document.open(); document.write(news.title); document.write(news.showPerson()); document.close(); }
說到對象就必需要知道js裏面的原型(prototype),它的做用是擴展任意對象(內建對象也能夠擴展)。爲指定對象增長屬性或者方法。java
這裏的擴展既有擴展類,又有擴展實例對象,他們之間的範圍域是不相同。數組
擴展類的方法/屬性:app
var article=function(title){ this.title = title; var arrys=new Array(); this.addPerson=function(person){ var i=arrys.length; arrys[i]=person; } this.showPerson=function(){ var persons=""; for(var i=0;i<arrys.length;i++){ persons+=arrys[i]; } return persons; } } window.onload=function(){ article.prototype.getTitles=function(name){return name;};//全部new出來的實例對象都使用這個方法 article.prototype.head="heihie";//全部實例對象都有這個屬性 var news=new article("nihao"); document.open(); document.write(news.getTitles("nihao")); document.close(); }
擴展實例對象:函數
var article=function(title){ this.title = title; var arrys=new Array(); this.addPerson=function(person){ var i=arrys.length; arrys[i]=person; } this.showPerson=function(){ var persons=""; for(var i=0;i<arrys.length;i++){ persons+=arrys[i]; } return persons; } } window.onload=function(){ var news=new article("nihao"); news.getTitles=function(name){return name;};//直接定義,不須要加上prototype對象,只有news纔有這個擴展方法 document.open(); document.writeln(news.getTitles("gg")); document.close(); }
必需要提一下,在對象中 this和var的區別//this修飾的屬性是共有屬性,var修飾的屬性是私有屬性。網站
新版本下getter/setter獲取和修改對象,有實體bean的感受this
function use(){ var chen=person.prototype; chen.__defineGetter__("title",function(){ return "title is "+this.myTitle;}); chen.__defineSetter__("title",function(tt){this.myTitle=tt;}); chen.print=println; var de=new person(); de.title="one title"; alert(de.title); } function println(){ document.writeln(this.title+" <br />") } //person 對象 function person(){ }
JS中的繼承/構造函數鏈.net
function Tune(title,type){ this.title=title; this.type=type; this.getTitle=function(){ return "song: "+this.title; } } function Ari_tune(title,type,artist){ this.artist=artist; //this.toString("Artist is "+artist); Tune.apply(this,arguments);//arguments是參數數組 Tune.call(this,title,type);能夠指定參數個數,更靈活一點 this.toString=function(){ return "artist: "+this.artist+" "+this.getTitle(); } } window.onload=function(){ Ari_tune.prototype=new Tune(); var song=new Ari_tune("i want to hold your hand","rock","beatles"); document.writeln(song.toString()); }
除了用類來建立對象,js中還有一次性對象的說法prototype
var oneOff={ name:"chen", age:18, method:function(name,age){ return "name is "+name+" age is "+age; } } window.onload=function(){ alert(oneOff.name); alert(oneOff.method("nihao",19)) var obj=new Object(); obj.name="name"; obj.age=19; obj.method=function(){ return "name is "+this.name+" age is "+this.age; }; alert(obj.method()); var objName=new function(){ this.name="nihao"; this.metdod=function(){ return "function "+this.name; } }; alert(objName.metdod()); }
function 與new function 區別 //Function 的區別一個很不錯的網站:code
http://www.jb51.net/article/7955.htm