學習目的:
1.Web相關開發越來越流行,學習JS十分有必要
2.多學習一種語言,想多瞭解一種語言的文化內涵
3.認識一下指令碼語言,之前一直學習C,C++,換換口味
學習途徑:
1.之前實習期間的項目積累
2.互連網的各種零碎的資料
3.codecademy的線上Js教學課程(冗長細緻的課程,打字打到手抽筋)
4.各種書本,如《headfirst Js》等
零星的感受:(一直在補充)
1.js中類的屬性,可以使用.也可以使用["xx"]來標識
2.JS也有封裝,在類的建構函式中使用 var來定義屬性或者方法而不是this
3.Js的函數定義之後沒有分好,但是變數定義之後有分號。
4.函數和類中的this不能省略
5.Js的執行個體化是通過 new 建構函式實現的。
function Person(name,age) {
[javascript]
this.name = name;
this.age = age;
}
// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
this.name = name;
this.age = age;
}
// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);6.使用prototype使得每個執行個體都有這個屬性,也實現了繼承
[javascript]
// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is "+this.name);
};
// define a Penguin class
function Penguin(name, numLegs) {
this.name = name;
this.numLegs = 2;
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
var penguin = new Penguin("Gigi");
penguin.sayName();
// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is "+this.name);
};
// define a Penguin class
function Penguin(name, numLegs) {
this.name = name;
this.numLegs = 2;
}
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();
var penguin = new Penguin("Gigi");
penguin.sayName();