明 光 大 正

JS补记

    Diary     Javascript

两种实现继承的方式

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    function Car() {
    var self = this;
    self.type = "Car"
    self.go = function() {
    console.log("Going...");
    };
    };
    Toyota = function() {};
    Toyota.prototype = new Car();
    Toyota.prototype.constructor = function() {
    var self = this;
    self.type = "Toyota";
    self.go = function() {
    console.log("A Toyota car is going...");
    }
    };
    Toyota.prototype.isJapaneseCar = true;
    var t = new Toyota();
    console.log(t instanceof Toyota);
    console.log(t instanceof Car);
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    var Car = Class.create({
    initialize: function() {
    this.type = "Car";
    },
    go: function() {
    console.log("Going...");
    }
    });
    var Toyota = Class.create(Car, {
    initialize: function() {
    this.type = "Toyota";
    this.isJapaneseCar = true;
    },
    go: function() {
    console.log("A Toyota car is going...");
    }
    });
    • 关于父类调用
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      var Toyota = Class.create(Car, {
      initialize: function() {
      this.type = "Toyota";
      this.isJapaneseCar = true;
      },
      go: function($super) {
      $super();
      console.log("A Toyota car is going...");
      }
      });

选择器

链表实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var Node = Class.create({
initialize: function (val) {
this.value = val;
this.next = null;
},
addChild: function(node) {
this.next = node;
}
});
var LinkedList = Class.create({
initialize: function () {
this.root = new Node(null);
this.size = 0;
},
add: function (object) {
obj = this.root
while (obj.next!= null) {
obj = obj.next;
}
obj.next = new Node(object)
}
});
页阅读量:  ・  站访问量:  ・  站访客数: