這篇文章主要介紹“JavaScript怎么創(chuàng)建對(duì)象并為對(duì)象添加方法和屬性”,在日常操作中,相信很多人在JavaScript怎么創(chuàng)建對(duì)象并為對(duì)象添加方法和屬性問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”JavaScript怎么創(chuàng)建對(duì)象并為對(duì)象添加方法和屬性”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新新互聯(lián),憑借10年的成都網(wǎng)站制作、網(wǎng)站建設(shè)經(jīng)驗(yàn),本著真心·誠(chéng)心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有千余家案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)。創(chuàng)建對(duì)象,為對(duì)象添加方法,屬性,動(dòng)態(tài)的添加和刪除,示例代碼如下:
{
var circle = {x:0,y:0,radius:2};
document.write("
圓心在:", circle.x," ",circle.y,"半徑為:",circle.radius);
document.write("
函數(shù)創(chuàng)建對(duì)象
");
function student(name,chinese,math,english)
{
this.name=name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.avg2=avg2;
}
//
function avg()
{
return ((this.chinese + this.math + this.english)/3);
}
function avg2()
{
return (((this.chinese + this.math + this.english)/3)+5);
}
}
{
var st1 = new student("zzc",88,95,20);
document.write("姓名"+ st1.name + "
");
document.write("語(yǔ)文"+ st1.chinese + "
");
document.write("數(shù)學(xué)"+ st1.math + "
");
document.write("英語(yǔ)"+ st1.english + "
");
st1.avg = avg;
var avgscore = st1.avg();
var avgscores = st1.avg2();
document.write("平均分"+ avgscore + "
");
document.write("平均分+5 = "+ avgscores + "
");
document.write("Delete object
");
document.write("st1對(duì)象屬性和方法:
");
for(var i in st1)
document.write(st1[i]+ "
");
document.write("
刪除對(duì)象的屬性(english)方法(avg2)
");
delete st1.avg2;
delete st1.english;
for(var i in st1)
document.write(st1[i]+ "
");
}
到此,關(guān)于“JavaScript怎么創(chuàng)建對(duì)象并為對(duì)象添加方法和屬性”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!