js繼承優化

在看《js設計模式》中,作者提到了js中的兩種繼承方式:類繼承 或 原型繼承,或許是本人才疏學淺,竟發現一些問題。

一、類繼承

思路:作者的思路是使用基于類來繼承,并且做了一個extend函數,在第一時間就吸引了我的眼球,函數如下:

1?function?extend(subClass,?superClass){
2??????var?F?=?function(){};
3??????F.prototype?=?superClass.prototype;
4??????subClass.prototype?=?new?F();
5??????subClass.prototype.constructor?=?subClass;
6?}

在js中sunClass和superClass分別是子類和父類的名字(即函數的名字).

在使用的時候作者的思路類似是這樣:

?1?function?Person()
?2?{
?3?????this.attr1?=?1;
?4?????this.attr2?=?2;
?5?????this.attr3?=?3;
?6?}
?7?
?8?function?Author()
?9?{
10?????Person.apply(this);
11?}
12?
13?extend(Author,Person);

?

但是我在測試中發現一個問題,在上面的extend函數的執行時間為0。仔細看看,其實這個類繼承僅僅的核心部分為Author函數中的

Person.apply(this);

extend函數并沒有起到作用?!

?

二、原型繼承

這種繼承基于一個父類的實例對象,只是把A.prorotype = obj;做了個函數封裝,得到以下的clone函數:

1?function?clone(parentObj)
2?{
3????function F()?=?{};
4????F.prototype?=?parentObj;
5????return?new?F();
6?}


?

轉載于:https://www.cnblogs.com/return/archive/2009/10/30/1593285.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279274.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279274.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279274.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python---[列表]lsit

內置數據結構(變量類型) -list -set -dict -tuple -list(列表) -一組又順序的數據組合 -創建列表 -空列表 list1 []        print(type(list1))        print(list1)        list2 [100]       …

喚醒計算機運行此任務_如何停止Windows 8喚醒計算機以運行維護

喚醒計算機運行此任務Windows 8 comes with a new hybrid boot system, this means that your PC is never really off. It also means that Windows has the permission to wake your PC as it needs. Here’s how to stop it from waking up your PC to do maintenance tasks…

轉整型_SPI轉can芯片CSM300詳解、Linux驅動移植調試筆記

一口君最近移植了一款SPI轉CAN的芯片CSM300A,在這里和大家做個分享。一、CSM300概述CSM300(A)系列是一款可以支持 SPI / UART 接口的CAN模塊。1. 簡介CSM300(A)系列隔離 SPI / UART 轉 CAN 模塊是集成微處理器、 CAN 收發器、 DC-DC 隔離電源、 信號隔離于一體的通信…

matlab練習程序(二值圖像連通區域標記法,一步法)

這個只需要遍歷一次圖像就能夠完全標記了。我主要參考了WIKI和這位兄弟的博客,這兩個把原理基本上該介紹的都介紹過了,我也不多說什么了。一步法代碼相比兩步法真是清晰又好看,似乎真的比兩步法要好很多。 代碼如下: clear all; c…

pc微信不支持flash_在出售PC之前,如何取消對Flash內容的授權

pc微信不支持flashWhen it comes to selling your old digital equipment you usually should wipe it of all digital traces with something like DBAN, however if you can’t there are some precautions you should take–here’s one related to Flash content you may h…

博客在線——Wireshark基本用法

http://blog.jobbole.com/ http://blog.jobbole.com/70907/轉載于:https://www.cnblogs.com/zhongbokun/p/9709326.html

繪制三維散點圖_SPSS統計作圖教程:三維散點圖

作者:豆沙包;審稿:張耀文1、問題與數據最大攜氧能力是身體健康的一項重要指標,但檢測該指標成本較高。研究者想根據性別、年齡、體重、運動后心率等指標建立預測最大攜氧能力的模型,招募了100名研究對象,測…

【Python】插入sqlite數據庫

import sqlite3 from datetime import datetimeconn sqlite3.connect(data.db) print("Opened database successfully")for i in range(100):time datetime.now()conn.execute("INSERT INTO test(time,url,imgPath) VALUES (?,?,?)", (time, "ww…

java數組轉list(Arrays .asList)

習慣性的錯誤代碼&#xff1a; Integer[] intArr {1,2,3}; List<Integer> lst Arrays .asList(intArr); lst.add(4); 報UnsupportedOperationException異常&#xff0c;原因是Arrays .asList() 返回的固定大小的列表&#xff0c;無法進行add、remove等操作&#xff1b;…

使用lodash防抖_什么,lodash 的防抖失效了?

戳藍字「前端技術優選」關注我們哦&#xff01;作者&#xff1a;yeyan1996https://juejin.im/post/6892577964458770445應某人的要求被迫營業&#xff0c;望各位看官不要吝嗇手中的贊-。-背景在使用 uni-app 開發小程序時&#xff0c;有個填寫表單的需求&#xff0c;包含兩個輸…

Ubuntu 12.10中的8個新功能,Quantal Quetzal

Ubuntu 12.10 has been released and you can download it now. From better integration with web apps and online services to improvements in Unity, there are quite a few changes – although none of them are huge or groundbreaking. Ubuntu 12.10已發布&#xff0c…

背單詞APP調研分析

前言&#xff1a;隨著我國網絡經濟重心向移動端的轉移&#xff0c;移動教育領域獲得的關注度在持續放大。互聯網的發展和移動設備的普及&#xff0c;我們開始在移動設備上學習&#xff0c;各種學習教育軟件如雨后春筍&#xff0c;越來越多&#xff0c;就背單詞軟件來說&#xf…

linux 提取cpio_Linux提取RPM包文件(cpio命令)詳解

在講解如何從 RPM 包中提取文件之前&#xff0c;先來系統學習一下 cpio 命令。cpio 命令用于從歸檔包中存入和讀取文件&#xff0c;換句話說&#xff0c;cpio 命令可以從歸檔包中提取文件(或目錄)&#xff0c;也可以將文件(或目錄)復制到歸檔包中。歸檔包&#xff0c;也可稱為文…

cdh中使用hue使用教程_我可以在戶外使用Philips Hue燈泡嗎?

cdh中使用hue使用教程Philips Hue lights are great to have in your house, and they can add a lot of convenience to your living space. However, what if you want to use these smart bulbs outdoors in porch lights or flood lights? Will Philips Hue bulbs work pr…

django (三) admin后臺系統

admin后臺系統 1. 安裝MySQL 1,安裝mysql: sudo apt install mysql-server (安裝過程中輸入密碼并牢記) 2,安裝后進入mysql: mysql -u用戶名 -p密碼 mysql -uroot -proot ? 3,在Django中配置和使用mysql數據庫 使用mysql數據庫&#xff0c;settings中配置如下&#xff1a; DAT…

python博客訪問量_史詩級干貨-python爬蟲之增加CSDN訪問量

AI人工智能史詩級干貨-python爬蟲之增加CSDN訪問量史詩級干貨-python爬蟲之增加CSDN訪問量搜索微信公眾號:‘AI-ming3526’或者’計算機視覺這件小事’ 獲取更多算法、機器學習干貨csdn&#xff1a;https://blog.csdn.net/baidu_31657889/github&#xff1a;https://github.com…

弄斷過河電纜_你說的是:剪斷電纜線

弄斷過河電纜Earlier this week we asked you if you’d cut the cable and switched to alternate media sources to get your movie and TV fix. You responded and we’re back with a What You Said roundup. 本周早些時候&#xff0c;我們問您是否要切斷電纜并切換到其他媒…

復制粘貼的句子

Today you do things people will not do,tomorrow you will do things people can not do. 你今天做別人不愿做的事&#xff0c;明天就能做別人做不到的事。轉載于:https://www.cnblogs.com/wensens/p/9723998.html

路由銷毀上一頁_路由器原理(數據通信)

路由&#xff1a;對數據包選擇路徑的過程路由器(也叫網關)智能選擇數據傳輸路由的設備&#xff0c;其端口數量較少&#xff01;功能&#xff1a;連接網絡1.連接異構網絡以太網、ATM網絡、FDDI網絡2.連接遠程網絡局域網、廣域網隔離廣播將廣播隔離在局域網內路由選擇網絡安全地址…

您可能沒有使用的最佳三星Galaxy功能

Samsung packs its flagship phones with a slew of features—some are even better than stock Android. Either way, there are a lot of things on these phones that you may not be using. Here are some of the best. 包三星旗艦手機用的特性-擺有的甚至比普通的Android…