人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的JavaScript学习笔记:创建对象和构造类懂设计的网友们快点来了解吧!
【 tulaoshi.com - Web开发 】
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)创建一个对象
script type="text/javaScript" var newObject=new Object(); //创建一个对象newObject.firstName="frank";//增加一个firstName属性newObject.sayName=function(){alert(this.firstName);} //添加一个sayName方法//调用sayName方法//newObject.sayName();//newObject["sayName"]();var FirstName=newObject["firstName"];var whatFunction;//if(whatVolume==1){//whatFunction="sayName";//}else if(whatVolume==2){//whatFunction="sayLoudly"//}//newObject[whatFunction]();function sayLoudly(){alert(this.firstName.toUpperCase());}newObject.sayLoudly=sayLoudly;//另一种方式添加方法newObject["sayLoudly"](); /script
function sayLoudly(){alert(this.firstName.toUpperCase());}var newObject={firstName:"frank",sayName:function(){alert(this.firstName);},sayLoudly:sayLoudly};//也可以这样 var newObject={firstName:"frank",sayName:function(){alert(this.firstName);},sayLoudly:sayLoudly,lastName:{lastName:"ziggy",sayName:function(){alert(this.lastName);}}};newObject.lastName.sayName();
function sayLoudly(){alert(this.name.toUpperCase());} function sayName(){ alert(this.name); }var newObject={name:"frank",sayName:sayName,sayLoudly:sayLoudly,lastName:{name:"ziggy",sayName:sayName}};newObject.lastName.sayName();
function newClass(){alert("constructor");this.firstName="frank";this.sayName=function(){alert(this.firstName);} // return this;}//var nc=newClass();var nc=new newClass();//nc.firstName="ziggy"; is oknc.sayName();
function newClass(){ this.firstName="frank"; }newClass.prototype.sayName=function(){alert(this.firstName);}var nc=new newClass();nc.firstName="ziggy";nc.sayName();var nc2=new newClass();nc2.sayName();
来源:http://www.tulaoshi.com/n/20160220/1633031.html
看过《JavaScript学习笔记:创建对象和构造类》的人还看了以下文章 更多>>