javaScript筆記(一)

來源:互聯網
上載者:User

javaScript物件導向編程:
var Cat = {

    name = '';
    color = '';
}
 
var cat1 = {};
cat1.name = "大毛";
cat1.color="黃色";

var cat2 = {};
cat2.name="二毛";
cat2.color = "黑色";

原始模式的改變:
function Cat(name,color){

    return {
      name:name,
      color:color
     
    }
}
產生執行個體對象:
var Cat1 = Cat(“大毛”,“黃色”);
var Cat2 = Cat("二毛",“黑色”);

建構函式模式:
function Cat(name,color){ //使用建構函式
     this.name = name;
    this.color = color;
}
var cat1 = new Cat("大毛","黃色");
var cat2 = new Cat("二毛","黑色");
alert(cat1.name); //大毛
alert(cat2.color); //黑色
但是建構函式模式會浪費記憶體

prototype模式:將那些不變的書信和方法,直接定義在prototype 對象上。
function Cat(name,color){
     this.name = name;
     this.color = color;
}
Cat.prototype.type = "貓科";
Cat.prototype.eat = function(){alert("吃老鼠")};
產生執行個體:
var cat1 = new Cat("大毛",“黃色”);
var cat2 = new Cat("二毛",“黑色”);

alert(cat1.type);
cat1.eat();

isPrototypeOf()判斷某個prototype和某個執行個體之間的關係。

hasOwnProperty()判斷某個prototype和某一個屬性是不是區域屬性例
例如:alert(cat1.hasOwnProperty("name"));結果是true

in 運算子:判斷某個執行個體是否含有某個屬性。
   alert("name" in cat1)l; //true
in運算子還可以用來遍曆某個對象的所有屬性
    for(var prop in cat1){alert("cat1["+prop+"]="+cat1[prop])}

如何產生一個繼承多個對象的執行個體:
現有動物對象的建構函式:
function Animal(){
    this.species = "動物";
}

還有一個貓的對象的建構函式,
function Cat(name,color){
    this.name= name;
    this.color = color;
}
怎樣使貓繼承動物呢?
1.用建構函式綁定呀:使用call,apply方法,將父物件的建構函式綁定在子物件上。
function Cat(name,color){
     Animal.apply(this,argumengts);
     this.name = name;
    this.color =color;
}
var cat1 = new Cat("大毛",“黃色”);
alert(cat1.species); //動物
2.更常見的方法是:prototype模式
如:貓的prototype對象指向了Animal的執行個體,就能整合animal了。
Cat.prototype = new Animal();
Cat.prototype.construtor = Cat;
var cat1 = new Cat("大毛","黃色”);
alert(cat1.species); //動物
3.直接繼承prototype
由於不變的屬性可以直接寫入prototype,所以我們讓cat()直接繼承Animal.prototype;
function Animal(){}
Animal.prototype.specoes = "動物";
Cat.prototype = Animal.prototype;
Cat.prototype.construtor = Cat;
var cat1 = new Cat("大毛","黃色");
alert(cat1.species);//動物
這種方法呢,將Animal的prototype也改掉了,所以又缺陷,
4,為瞭解決這個缺陷呢,所以利用一個Null 物件作為中介,
var F = function(){}
F.prototype = Animal.prototype;
Cat.prototype = new F();
Cat.prototype.construtor = Cat;
alert(Animal.prototype.construtor);//Animal
5.prototype 模式的封裝函數
我們講上面的方法封裝成一個函數,便於使用
function extend(Child,Parent){
    car F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.construtor = Child;
Child.uber = Parent.prototype;

}
使用這個函數的方法如下:
extend(Cat,Animal);
var cat1 = new Cat("大毛","黃色");
alert(cat1.specoes);
6.拷貝繼承
如果把父物件的所有屬性和法國法,拷貝進子物件的話,也能實現繼承。

function Animal(){}
Animal.prototype.species = "動物";
function extends(Child,Parent){
    var p = Parent.prototype;
    var c = Child.prototype;
for(car i in p ){
     c[i] = p[i];
  }
c.uber = p;
}使用的時候這樣寫
extends(Cat,Animal);
var cat1 = new Cat("大毛","黃色");
alert(cat1.species) ; //動物

接下來我們不使用建構函式實現繼承。
var Chinese = {nation:'中國人'}
var Doctor = {carrer:'醫生'}
怎樣讓醫生繼承中國人呢,
object()方法:
function object(o){
    function F(){}
    F.prototype = o;//把子物件的prototype屬性指向了父物件,使之連在一起,

     return new F();
}
使用方法
var Doctor = object(Chinese);
Doctor.career = "醫生";//加上子物件的本身的屬性
alert(Doctor.nation); //中國人
還有一種方法:淺拷貝
下面的這個函數就是在拷貝:
function extendCopy(p){
    var c = {};
for (var i in p){
  c[i] = p[i];
}
c.uber = p;
return c;

}
使用的時候這樣寫:
var Doctor = extendCopy(chinese);
Doctor.career = "醫生";
alert(Doctor.nation);//中國

這樣存在一個問題,就是子物件只是一個記憶體位址,不是真正的拷貝,,這樣會改變父物件的值
深拷貝:
即使能夠實現真正意義上的數組和對象的拷貝,
function deepCopy(p,c){
    var v = c||{};
for(var i in p ){

    if(typrof p[i] == 'object'){
        c[i] = (p[i].construtor == Array)?[]:{
       }else{
        c[i] = p[i];
       }
     return c;
}

}
使用的時候:
var Doctor = deepCopre(Chinese);
Chinese.bithplace = ['北京',‘上海’,‘廈門’];
Doctor.birthplace .push('香港');
這樣父物件就不會受到影響了,
alert(Doctor.birthplace);//北京上海廈門香港
alert(Chinese.birthplace);//北京上海,廈門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.