Javascript OOP之物件導向_基礎知識

來源:互聯網
上載者:User

物件導向程式設計(Object-oriented programming,OOP)是一種程式設計範型,同時也是一種程式開發的方法。對象指的是類的執行個體。它將對象作為程式的基本單元,將程式和資料封裝其中,以提高軟體的重用性、靈活性和擴充性。——維基百科

一般物件導向包含:繼承,封裝,多態,抽象

對象形式的繼承

淺拷貝

var Person = {  name: 'allin',  age: 18,  address: {    home: 'home',    office: 'office',  }  sclools: ['x','z'],};var programer = {  language: 'js',};function extend(p, c){  var c = c || {};  for( var prop in p){    c[prop] = p[prop];  }}extend(Person, programer);programer.name; // allinprogramer.address.home; // homeprogramer.address.home = 'house'; //housePerson.address.home; // house

從上面的結果看出,淺拷貝的缺陷在於修改了子物件中參考型別的值,會影響到父物件中的值,因為在淺拷貝中對參考型別的拷貝只是拷貝了地址,指向了記憶體中同一個副本。

深拷貝

function extendDeeply(p, c){  var c = c || {};  for (var prop in p){    if(typeof p[prop] === "object"){      c[prop] = (p[prop].constructor === Array)?[]:{};      extendDeeply(p[prop], c[prop]);    }else{      c[prop] = p[prop];    }  }}

利用遞迴進行深拷貝,這樣子物件的修改就不會影響到父物件。

extendDeeply(Person, programer);programer.address.home = 'allin';Person.address.home; // home利用call和apply繼承function Parent(){  this.name = "abc";  this.address = {home: "home"};}function Child(){  Parent.call(this);  this.language = "js"; }ES5中的Object.create()var p = { name : 'allin'};var obj = Object.create(o);obj.name; // allin

Object.create()作為new操作符的替代方案是ES5之後才出來的。我們也可以自己類比該方法:

//類比Object.create()方法function myCreate(o){  function F(){};  F.prototype = o;  o = new F();  return o;}var p = { name : 'allin'};var obj = myCreate(o);obj.name; // allin

目前,各大瀏覽器的最新版本(包括IE9)都部署了這個方法。如果遇到老式瀏覽器,可以用下面的代碼自行部署。

  if (!Object.create) {    Object.create = function (o) {       function F() {}      F.prototype = o;      return new F();    };  }

類的繼承

Object.create()function Person(name, age){}Person.prototype.headCount = 1;Person.prototype.eat = function(){  console.log('eating...');}function Programmer(name, age, title){}Programmer.prototype = Object.create(Person.prototype); //建立繼承關係Programmer.prototype.constructor = Programmer; // 修改constructor的指向

調用父類方法

function Person(name, age){  this.name = name;  this.age = age;}Person.prototype.headCount = 1;Person.prototype.eat = function(){  console.log('eating...');}function Programmer(name, age, title){  Person.apply(this, arguments); // 調用父類的構造器}Programmer.prototype = Object.create(Person.prototype);Programmer.prototype.constructor = Programmer;Programmer.prototype.language = "js";Programmer.prototype.work = function(){  console.log('i am working code in '+ this.language);  Person.prototype.eat.apply(this, arguments); // 調用父類上的方法}

封裝

命名空間

js是沒有命名空間的,因此可以用對象類比。

var app = {}; // 命名空間app//模組1app.module1 = {  name: 'allin',  f: function(){    console.log('hi robot');  }};app.module1.name; // "allin"app.module1.f(); // hi robot

靜態成員

function Person(name){  var age = 100;  this.name = name;}//靜態成員Person.walk = function(){  console.log('static');};Person.walk(); // static

私人與公有

function Person(id){  // 私人屬性與方法  var name = 'allin';  var work = function(){    console.log(this.id);  };  //公有屬性與方法  this.id = id;  this.say = function(){    console.log('say hello');    work.call(this);  };};var p1 = new Person(123);p1.name; // undefinedp1.id; // 123p1.say(); // say hello 123

模組化

var moduleA;moduleA = function() {  var prop = 1;  function func() {}  return {    func: func,    prop: prop  };}(); // 立即執行匿名函數

prop,func 不會被泄露到全域範圍。或者另一種寫法,使用 new

moduleA = new function() {  var prop = 1;  function func() {}  this.func = func;  this.prop = prop;}

多態

類比方法重載

arguments屬性可以取得函數調用的實參個數,可以利用這一點類比方法的重載。

function demo(a, b ){  console.log(demo.length); // 得到形參個數  console.log(arguments.length); //得到實參個數  console.log(arguments[0]); // 第一個實參 4  console.log(arguments[1]); // 第二個實參 5}demo(4, 5, 6);//實現可變長度實參的相加function add(){  var total = 0;  for( var i = arguments.length - 1; i >= 0; i--){    total += arguments[i];  }  return total;}console.log(add(1)); // 1console.log(add(1, 2, 3)); // 7// 參數不同的情況function fontSize(){  var ele = document.getElementById('js');  if(arguments.length == 0){    return ele.style.fontSize;  }else{    ele.style.fontSize = arguments[0];  }}fontSize(18);console.log(fontSize());// 類型不同的情況function setting(){  var ele = document.getElementById('js');  if(typeof arguments[0] === "object"){    for(var p in arguments[0]){      ele.style[p] = arguments[0][p];    }  }else{    ele.style.fontSize = arguments[0];    ele.style.backgroundColor = arguments[1];  }}setting(18, 'red');setting({fontSize:20, backgroundColor: 'green'});

方法重寫

function F(){}var f = new F();F.prototype.run = function(){  console.log('F');}f.run(); // Ff.run = function(){  console.log('fff');}f.run(); // fff

抽象類別

在構造器中 throw new Error(''); 拋異常。這樣防止這個類被直接調用。

function DetectorBase() {  throw new Error('Abstract class can not be invoked directly!');}DetectorBase.prototype.detect = function() {  console.log('Detection starting...');};DetectorBase.prototype.stop = function() {  console.log('Detection stopped.');};DetectorBase.prototype.init = function() {  throw new Error('Error');};// var d = new DetectorBase();// Uncaught Error: Abstract class can not be invoked directly!function LinkDetector() {}LinkDetector.prototype = Object.create(DetectorBase.prototype);LinkDetector.prototype.constructor = LinkDetector;var l = new LinkDetector();console.log(l); //LinkDetector {}__proto__: LinkDetectorl.detect(); //Detection starting...l.init(); //Uncaught Error: Error

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.