阅读Ext 学习Javascript(二)Core/Ext.extend 从继承说起

本文深入探讨JavaScript中的继承机制,通过具体实例介绍了如何实现类的继承,并解析了Ext框架中的extend方法,展示了如何利用继承来扩展类的功能。
一般的,如果我们定义一个类,会定义一个function对象,然后将公用方法写到其原型上,例如: var Tiger = function(){} Tiger.prototype.Hunting = function(){} 但是要建立一个完善的框架或者类库,没有继承帮忙,组织代码将是一件非常辛苦且难以管理的工作。Js中的类是function对象,实现继 承,主要要将子类的原型设置为父类的一个实例(这样子类就用有了父类原型的所有成员),并重新将子类原型的构造器设置为子类自己。如以下代码所示: function Animal(){} function Tiger(){} Tiger.prototype = new Animal() Tiger.prototype.constructor = Tiger 实现继承并不难,将上面的Animal和Tiger参数化封装为一个方法就可以实现(当然实际应用中就要复制一些了),代码如下: function Extend(subFn, superFn){ subFn.prototype = new superFn() subFn.prototype.constructor = subFn } Ext作为一个优秀的框架,当然也少不了继承的实现。如前一篇文章所谈到的,现在让我们一行行代码理解Ext.extend extend : function(){ // inline overrides var io = function(o){ for(var m in o){ this[m] = o[m]; } }; return function(sb, sp, overrides){ if(typeof sp == 'object'){ overrides = sp; sp = sb; sb = function(){sp.apply(this, arguments);}; } var F = function(){}, sbp, spp = sp.prototype; F.prototype = spp; sbp = sb.prototype = new F(); sbp.constructor=sb; sb.superclass=spp; if(spp.constructor == Object.prototype.constructor){ spp.constructor=sp; } sb.override = function(o){ Ext.override(sb, o); }; sbp.override = io; Ext.override(sb, overrides); return sb; }; }() 本来只有两行代码就可以实现的继承变成了近30行,Ext都做了什么呢?通常情况下只传入两个类型的话(subFn和superFn),上面的代码将简化为 extend : function(){ // inline overrides var io = function(o){ for(var m in o){ this[m] = o[m]; } }; return function(sb, sp, overrides){ var F = function(){}, sbp, spp = sp.prototype; F.prototype = spp; sbp = sb.prototype = new F(); sbp.constructor=sb; sb.superclass=spp; sb.override = function(o){ Ext.override(sb, o); }; sbp.override = io; Ext.override(sb, overrides); return sb; }; }() 定义一个空函数,将其原型设置为sp的原型spp,其实F就是sp的一个替身,理解的时候可以认为就是sp。将子类sb的原型设置为F的一个 实例,然后再将其原型的构造器设置为自己sb,为了方便找到父类sp,在子类sb上添加了一个superclass属性为父类sp的原型spp。为了方便 扩展属性,在子类sb上添加了属性重写的override方法,也在其原型上添加了override方法(这样其所有实例对象就可以从一个对象重写现有属 性了)。
到这里算是对继承有了一些了解(不到位的地方在以后的阅读中继续加强)。好了,有了继承的支持,我们就可以加速类型的扩展了。 
Started by user administrator Obtained uat_tc/frontend/Jenkinsfile from git http://47.100.45.4:8060/pipeline/dbm-ci.git [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout The recommended git tool is: NONE using credential dbm-deploy > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://47.100.45.4:8060/pipeline/dbm-ci.git # timeout=10 Fetching upstream changes from http://47.100.45.4:8060/pipeline/dbm-ci.git > git --version # timeout=10 > git --version # 'git version 2.39.5' using GIT_ASKPASS to set credentials > git fetch --tags --force --progress -- http://47.100.45.4:8060/pipeline/dbm-ci.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 Checking out Revision 39c6e6fb53f67b249d725128e27fe95d10d8c634 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 39c6e6fb53f67b249d725128e27fe95d10d8c634 # timeout=10 Commit message: "前端部署脚本修改" > git rev-list --no-walk 39c6e6fb53f67b249d725128e27fe95d10d8c634 # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Tool Install) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] git The recommended git tool is: NONE using credential dbm-deploy > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git # timeout=10 Fetching upstream changes from http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git > git --version # timeout=10 > git --version # 'git version 2.39.5' using GIT_ASKPASS to set credentials > git fetch --tags --force --progress -- http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/uat^{commit} # timeout=10 Checking out Revision d96cc2d276749987f956838156865e2c99d68c70 (refs/remotes/origin/uat) > git config core.sparsecheckout # timeout=10 > git checkout -f d96cc2d276749987f956838156865e2c99d68c70 # timeout=10 > git branch -a -v --no-abbrev # timeout=10 > git checkout -b uat d96cc2d276749987f956838156865e2c99d68c70 # timeout=10 Commit message: "合并分支 'test' 到 'uat'" First time build. Skipping changelog. [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] sh + rm -rf node_modules + node -v v18.20.3 + git remote set-url origin https://github.com/evrone/postcss-px-to-viewport.git + npm install --registry=https://registry.npmmirror.com npm warn deprecated yaeti@0.0.6: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info. added 346 packages in 2s 77 packages are looking for funding run `npm fund` for details + npm run build:uat > cssccrane@0.0.0 build:uat > vite build --mode uat postcss-px-to-viewport: postcss.plugin was deprecated. Migration guide: https://evilmartians.com/chronicles/postcss-8-plugin-migration vite v5.4.20 building for uat... transforming... DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0. More info and automated migrator: https://sass-lang.com/d/import ╷ 1 │ @use "@/assets/css/constant.scss" as *;@import "./base.scss"; │ ^^^^^^^^^^^^^ ╵ src/assets/css/index.scss 1:48 root stylesheet DEPRECATION WARNING [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0. More info and automated migrator: https://sass-lang.com/d/import ╷ 2 │ @import "./constant.scss"; │ ^^^^^^^^^^^^^^^^^ ╵ src/assets/css/index.scss 2:9 root stylesheet DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api ✓ 430 modules transformed. Generated an empty chunk: "@antfu". Generated an empty chunk: "vue". Generated an empty chunk: "vue-demi". x Build failed in 1.87s error during build: [vite:esbuild-transpile] Transform failed with 2 errors: assets/vant-!~{00b}~.js:2878:6: ERROR: The symbol "bem" has already been declared assets/vant-!~{00b}~.js:5104:6: ERROR: The symbol "bem" has already been declared  The symbol "bem" has already been declared 2876| const Tabs = withInstall(stdin_default$f); 2877| 2878| const [name$c, bem] = createNamespace("picker-group"); | ^ 2879| const PICKER_GROUP_KEY = Symbol(name$c); 2880| extend({ The symbol "bem" has already been declared 5102| const Notify = withInstall(stdin_default$2); 5103| 5104| const [name$1, bem, t] = createNamespace("search"); | ^ 5105| const searchProps = extend({}, fieldSharedProps, { 5106| label: String,  at failureErrorWithLog (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:1472:15) at /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:755:50 at responseCallbacks.<computed> (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:622:9) at handleIncomingPacket (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:677:12) at Socket.readFromStdout (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:600:7) at Socket.emit (node:events:517:28) at addChunk (node:internal/streams/readable:368:12) at readableAddChunk (node:internal/streams/readable:341:9) at Readable.push (node:internal/streams/readable:278:10) at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) Stage "Deploy" skipped due to earlier failure(s) [Pipeline] getContext [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
09-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值