关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享JavaScript入门教程(12):对象化编程教程,希望对大家能有一点小小的帮助。
【 tulaoshi.com - Web开发 】
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)
关于对象化编程的语句 现在我们有实力学习以下关于对象化编程,但其实属于上一章的内容了。
with 语句 为一个或一组语句指定默认对象。
用法: 
with (对象) 语句;x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10);
y = Math.tan(14 * Math.E);with (Math) {
  x = cos(3 * PI) + sin(LN10);
  y = tan(14 * E);
}script
...
function check(formObj) {
  ...
}
...
/script
body ...
...
form ...
...
input type="text" ... onchange="check(this.form)"
...
/form
...
/bodyfunction 构造函数名 [(参数)] {
  ...
  this.属性名 = 初始值;
  ...
}var 变量名 = new 构造函数名[(参数)];function Is() {
  var agent = navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);  //主版本号
  this.minor = parseFloat(navigator.appVersion);//全版本号
  this.ns = ((agent.indexOf('mozilla')!=-1) &&
             ((agent.indexOf('spoofer')==-1) && //是否 Netscape
              (agent.indexOf('compatible') == -1)));
  this.ns2 = (this.ns && (this.major == 3));    //是否 Netscape 2
  this.ns3 = (this.ns && (this.major == 3));    //是否 Netscape 3
  this.ns4b = (this.ns && (this.minor  4.04)); //是否 Netscape 4 低版本
  this.ns4 = (this.ns && (this.major = 4));    //是否 Netscape 4 高版本
  this.ie = (agent.indexOf("msie") != -1);      //是否 IE
  this.ie3 = (this.ie && (this.major == 2));    //是否 IE 3
  this.ie4 = (this.ie && (this.major = 4));    //是否 IE 4
  this.op3 = (agent.indexOf("opera") != -1);    //是否 Opera 3
  this.win = (agent.indexOf("win")!=-1);        //是否 Windows 版本
  this.mac = (agent.indexOf("mac")!=-1);        //是否 Macintosh 版本
  this.unix = (agent.indexOf("x11")!=-1);       //是否 Unix 版本
}
var is = new Is();function myFriend(theName, gender, theAge, birthOn, theJob) {
  this.name = theName;
  this.isMale = (gender.toLowerCase == 'male');
  this.age = theAge;
  this.birthday = new Date(birthOn);
  this.job = theJob
}
var Stephen = new myFriend('Stephen', 'Male', 18, 'Dec 22, 1982', 'Student');来源:http://www.tulaoshi.com/n/20160220/1632831.html
看过《JavaScript入门教程(12):对象化编程》的人还看了以下文章 更多>>