JS學習筆記

          Posted on 2007-09-14 08:35 puras 閱讀(1708) 評論(0)  編輯  收藏 所屬分類: JavaScript

          作者:赫連紫軒(puras)


          最近在看ExtJS,jQuery這些JS的框架
          發現里面有很多基礎的東西居然都不明白
          無奈只能找本書復習復習基礎啦
          隨手就記了點

          • 共有5種原始類型:Undefined, Null, Boolean, Number和String
          • 使用typeof運算符來判斷一個值的類型
          • 值null和值undefined是相等的
          • NaN不能用于計算,并與自身不相等
          • 位運算NOT實質上是對數字求負,然后減1,因此,25變為-26
          • 如果函數無明確的返回值,或調用了沒有參數的return語句,那么它真正返回的值是undefined

           

          定義類或對象的方式


          工廠方式:

          function showColor() {
              alert(
          this.color);
          }
           

          function createCar(sColor, iDoors, iMpg) {
              
          var oTempCar = {};
              oTempCar.color 
          = sColor;
              oTempCar.doors 
          = iDoors;
              oTempCar.mpg 
          = iMpg;
              oTempCar.showColor 
          = showColor; 

              
          return oTempCar;
          }
           

          var oCar1 = createCar('red', 423);
          var oCar2 = createCar('blue', 225);



          構造函數方式:

          function showColor() {
              alert(
          this.color);
          }
           

          function Car(sColor, iDoors, iMpg) {
              
          this.color = sColor;
              
          this.doors = iDoors;
              
          this.mpg = iMpg;
              
          this.showColor = showColor;
          }
           

          var oCar1 = new Car('red', 423);
          var oCar2 = new Car('blue', 225);



          原型方式:

          function Car() {} 

          Car.prototype.color 
          = 'red';
          Car.prototype.doors 
          = 4;
          Car.prototype.mpg 
          = 23;
          Car.prototype.showColor 
          = function() {
              alert(
          this.color);
          }


          var oCar1 = new Car();
          var oCar2 = new Car();



          混合的構造函數/原型方式(推薦使用):

          function Car(sColor, iDoors, iMpg) {
              
          this.color = sColor;
              
          this.doors = iDoors;
              
          this.mpg = iMpg;
          }

          Car.prototype.showColor 
          = function() {
              alert(
          this.color);
          }


          var oCar1 = new Car('red', 423);
          var oCar2 = new Car('blue', 225);



          動態原型方法:

          function Car(sColor, iDoors, iMpg) {
              
          this.color = sColor;
              
          this.doors = iDoors;
              
          this.mpg = iMpg; 

              
          if (typeof Car._initialized == 'undefined') {
                  Car.prototype.showColor 
          = function() {
                      alert(
          this.color);
                  }
           

                  Car._initialized 
          = true;
              }

          }




          混合工廠方法:

          function Car() {
              
          var oTempCar = new Object();
              oTempCar.color 
          = 'red';
              oTempCar.doors 
          = 4;
              oTempCar.mpg 
          = 23;
              oTempCar.showColor 
          = function() {
                  alert(
          this.color);
              }


              
          return oTempCar;
          }

          var car = new Car();




          繼承的方式


          對象冒充:

          function ClassA(sColor) {
              
          this.color = sColor;
              
          this.sayColor = function() {
                  alert(
          this.color);
              }

          }
           

          function ClassB(sColor, sName) {
              
          this.newMethod = ClassA;
              
          this.newMethod(sColor);
              
          delete this.newMethod;
              
          this.name = sName;
              
          this.sayName = function() {
                  alert(
          this.name);
              }

          }




          子類的新屬性和方法必須在刪除父類之后定義

          call()方法:

          function ClassA(sColor) {
              
          this.color = sColor;
              
          this.sayColor = function() {
                  alert(
          this.color);
              }

          }
           

          function ClassB(sColor, sName) {
              ClassA.call(
          this, sColor);
              
          this.name = sName;
              
          this.sayName = function() {
                  alert(
          this.name);
              }

          }




          apply()方法:

          function ClassA(sColor) {
              
          this.color = sColor;
              
          this.sayColor = function() {
                  alert(
          this.color);
              }

          }
           

          function ClassB(sColor, sName) {
              ClassA.call(
          thisnew Array(sColor));
              
          //ClassA.call(this, arguments); //當父類和子類的參數順序完全一致時可使用此方法
              this.name = sName;
              
          this.sayName = function() {
                  alert(
          this.name);
              }

          }




          原型鏈:

          function ClassA() {
          }
           

          ClassA.prototype.color
          ='red';
          ClassA.prototype.sayColor 
          = function() {
              alert(
          this.color);
          }


          function ClassB() {
          }
           

          ClassB.prototype 
          = new ClassA(); 

          ClassB.prototype.name 
          = '';
          ClassB.prototype.sayName 
          = function() {
              alert(
          this.name);
          }
          ;



          混合方式:

          function ClassA(sColor) {
              
          this.color = sColor;
          }
           

          ClassA.prototype.sayColor 
          = function() {
              alert(
          this.color);
          }


          function ClassB(sColor, sName) {
              ClassA.call(
          this, sColor);
              
          this.name = sName;
          }
           

          ClassB.prototype 
          = new ClassA(); 

          ClassB.prototype.sayName 
          = function() {
              alert(
          this.name);
          }




          朋友寫的一種繼承方式,感覺比上面幾種都要好:

          Function.prototype.extend = function (Base) {
              
          var Prototype = function () {};
              Prototype.prototype 
          = Base.prototype;
              
          this.prototype = new Prototype();
              
          this.prototype.base = Base;
              
          this.prototype.constructor = this;
          }

          function Bird(name) {
              
          this.name = name;
          }
           

          Bird.prototype.fly 
          = function() {
              alert(
          this.name + " is flying");
          }
           

          function Duck(name, owner) {
              
          this.base(name);
              
          this.owner = owner;
          }

          Duck.extend(Bird); 

          Duck.prototype.fly 
          = function () {
              alert(
          this.name + " is a duck!");
          }
           

          new Duck("duck A").fly();
          但這種寫法有個問題
          就是無法多層繼承
          雖然又加了點東西可以實現多層繼承了
          但還是不如上面其他方式的寫法好(個人感覺)

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 47, comments - 124, trackbacks - 0, articles - 0

          Copyright © puras

          主站蜘蛛池模板: 秀山| 浠水县| 闵行区| 东源县| 姜堰市| 南汇区| 德兴市| 新邵县| 望奎县| 哈尔滨市| 武川县| 乐安县| 威海市| 万载县| 木里| 屏山县| 阿拉善左旗| 公安县| 新河县| 独山县| 江都市| 麦盖提县| 密云县| 景德镇市| 阿勒泰市| 崇义县| 田林县| 平罗县| 杭州市| 鹤山市| 那坡县| 历史| 根河市| 石狮市| 荣成市| 巩义市| 大余县| 嫩江县| 石林| 邵阳市| 福建省|