Ext.namespace解析

本文详细解析了Ext.js中Namespace的实现原理,通过对比使用与不使用Namespace的代码,解释了Namespace作为包管理机制的重要性,以及如何正确地定义和使用包来避免类名冲突。
对于这个函数,我一直不太清楚,今天查了查官方的解释,豁然开朗。

对于java中的包,我们都认为主要是为了类名重复而引入的一种管理类的方法。同样,因为js定义的类太多,难免会定义重复的类名。特别是在今天这个需要共同协作完成项目的世界里。Ext.namespace正是为此而生。

Ext.namespace('com.company');
就是定义了一个com.company的包。

com.company.ClassA = function(){};就是定义了com.company包下面的一个Class。你一定会问,既然定义了包,为什么还要写包的前缀 com.company?我直接写com.company.ClassA = function(){}不行吗?对,一开始我也有这样的疑惑,com.company.ClassA不就是一个变量名称吗?哪儿来的包。后来经实践,javascript的变量名称是不准包括'.'的。因此,直接写com.company.ClassA = function(){};在语法上是不正确的。

现在想起来Ext在namespace的解决方法了。查看ext的官方解释,如果我们不用Ext.namespace的话,创建命名空间应该这样。

if (!App) App = {};
if (!App.form) App.form = {};
if (!App.data) App.data = {};

而 Ext.namespace('App', 'App.form', 'App.data');
则跟上面3句话的作用是一样的。

可以看出,这个包其本质上是一个对象,一个window的变量。也就是说,我们的包是一个分层次的对象。如com.company,应该解释成com对象的一个属性 company。如果定义这个包下面的类,则应该把这个类添加到com对象的属性company的属性里面。

到此,ext的 namespace的实现机制基本上就出来了。尽管实现包的方法很牵强,但目前可能是一个比较好的解决方案了。
plugins { id 'com.android.application' id 'jacoco' } android { namespace "com.kotei.overseas.navi" compileSdkVersion rootProject.ext.android["compileSdkVersion"] defaultConfig { applicationId "com.kotei.overseas.navi" minSdkVersion rootProject.ext.android["minSdkVersion"] targetSdkVersion rootProject.ext.android["targetSdkVersion"] versionCode 1 versionName "S3_Naviapp_V0.8.4.0819_${releaseTime()}" //versionName "M18_Naviapp_V0.8.4.0819_${releaseTime()}" multiDexEnabled true manifestPlaceholders = [ useSystemUid: project.properties["USE_SYSTEM_UID"] == "true" ? "android.uid.system" : "" ] } signingConfigs { releaseConfig { keyAlias 'platform' keyPassword 'android' storeFile file(rootDir.getAbsolutePath() + '/KeyStore/kotei_navi.jks') storePassword 'android' v1SigningEnabled true v2SigningEnabled true } debugConfig { keyAlias 'platform' keyPassword 'android' storeFile file(rootDir.getAbsolutePath() + '/KeyStore/kotei_navi.jks') storePassword 'android' v1SigningEnabled true v2SigningEnabled true } } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } sourceSets { main { res.srcDirs = ['src/main/res' ] java.srcDirs = ['src/main/java', 'src/main/javaGson'] // 直接移除 androidTest 的源码目录 androidTest { java.srcDirs = [] } } } buildFeatures { aidl true dataBinding true buildConfig true } buildTypes { def buildConfigCommon = { type -> def currentVersionName = android.defaultConfig.versionName type.buildConfigField("String", "VERSION_INFO", "\"${type.name.toUpperCase()}; ${currentVersionName}; \"") // 日志模式配置 def logMode = type.name == "debug" ? 1 : 28 // debug模式=1,其他=28 type.buildConfigField("int", "LOG_MODE", "${logMode}") } release { ndk { abiFilters 'arm64-v8a' // 正式包只保留64位 } minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.releaseConfig buildConfigCommon(it) buildConfigField "String", "BUILD_TYPE", "\"release\"" } debug { //testCoverageEnabled true signingConfig signingConfigs.debugConfig buildConfigCommon(it) versionNameSuffix "-debug" buildConfigField "String", "BUILD_TYPE", "\"debug\"" } } android.applicationVariants.all { variant -> variant.outputs.all { //这里修改apk文件名 def buildType = variant.buildType.name def suffix = buildType == "debug" ? "debug" : "release" // 根据构建类型添加后缀 outputFileName = "${getAppName().replace(' ', '_')}_${suffix}_${releaseLongTime()}.apk" } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) implementation "androidx.viewpager2:viewpager2:1.0.0" implementation 'net.lingala.zip4j:zip4j:1.8.0' api rootProject.ext.dependencies["gson"] implementation rootProject.ext.support["appcompat-v7"] implementation rootProject.ext.support["viewmodel"] implementation rootProject.ext.support["livedata"] implementation rootProject.ext.support["databinding"] implementation rootProject.ext.support["material"] implementation rootProject.ext.support["fragment"] implementation rootProject.ext.dependencies["hutool-all"] implementation rootProject.ext.dependencies["mmkv"] implementation rootProject.ext.dependencies["AndroidAutoSize"] implementation rootProject.ext.dependencies["annotations"] implementation rootProject.ext.dependencies["eventbus"] implementation 'com.google.ar.sceneform:filament-android:1.17.1' annotationProcessor rootProject.ext.dependencies["glide_compiler"] implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' api 'com.github.bumptech.glide:glide:4.11.0' api 'com.google.zxing:core:3.3.0' implementation project(':sdk_here_navigate') if (getLocalProperty("isSdkModuleUiS31a", true)) { implementation(project(":sdk_module_uiskin")) implementation project(':sdk_module_ui_s31a') } if (getLocalProperty("isSdkModuleDistribute", true)) { implementation project(':sdk_module_distribute') } implementation project(':sdk_external_Interface') implementation rootProject.ext.dependencies["androidsvg"] implementation rootProject.ext.dependencies["core-ktx"] implementation rootProject.ext.dependencies["skin-support"] implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3" // Latest stable //协程 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3" // For Android //协程 implementation rootProject.ext.dependencies["guava"] implementation 'org.bouncycastle:bcprov-jdk15on:1.70' implementation 'org.bouncycastle:bcpkix-jdk15on:1.70' implementation 'com.squareup.okhttp3:okhttp:4.9.3' testImplementation rootProject.ext.support["junit"]; androidTestImplementation "androidx.test:core:" + rootProject.versions["test-core"]; androidTestImplementation "androidx.test.ext:junit:" + rootProject.versions["test-extJUnit"]; androidTestImplementation "androidx.test:runner:" + rootProject.versions["test-runner"]; androidTestImplementation "androidx.test.espresso:espresso-core:" + rootProject.versions["test-espresso-core"]; androidTestImplementation "androidx.test.espresso:espresso-contrib:" + rootProject.versions["test-espresso-contrib"]; androidTestImplementation "com.google.truth:truth:" + rootProject.versions["test-truth"]; } /** * 从根目录下的 local.properties 中提取指定的属性,若无则返回默认值 */ def getLocalProperty(String keyName, Object defaultValue) { File file = rootProject.file("local.properties") if (!file.exists() || keyName == null || keyName.length() == 0) { print("指定文件(local.properties)或者属性 $keyName 不存在") return defaultValue } InputStream inputStream = file.newDataInputStream() Properties properties = new Properties() properties.load(inputStream) def localKeys = properties.keys() for (String propertyName : localKeys) { if (keyName != propertyName) { continue } def localPropertyValue = properties.getProperty(keyName) if (defaultValue instanceof Integer) { return Integer.parseInt(localPropertyValue) } else if (defaultValue instanceof Double) { return Double.parseDouble(localPropertyValue) } else if (defaultValue instanceof Boolean) { return Boolean.parseBoolean(localPropertyValue) } else if (defaultValue instanceof String) { return localPropertyValue } else { println("未知类型属性$propertyName,请检查后重试") } } println("获取 $keyName 失败,返回默认值: $defaultValue") return defaultValue } /** * 更新项目根目录下 local.properties 的属性值 */ def updateLocalProperty(String keyName, String value) { def comment = """ ## This file must *NOT* be checked into Version Control Systems, # as it contains information specific to your local configuration. # # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. """ println("updateLocalProperty with: $keyName=$value") File file = rootProject.file("local.properties") if (!file.exists()) { file.createNewFile() } if (keyName == null || keyName.length() == 0) { println("指定文件(local.properties)或者属性 $keyName 不存在,设置属性失败") return } InputStream inputStream = file.newDataInputStream() Properties properties = new Properties() properties.load(inputStream) properties.setProperty(keyName, value) properties.store(file.newWriter(), comment) } static def getGitCommitDate() { Date date = new Date(); String formattedDate = date.format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("UTC")); println("getGitCommitDate -> " + formattedDate) return formattedDate } static def getVersionName() { return versionName; } static def getAppName() { return "M18 Naviapp" //return "S31 Naviapp"; } def static releaseLongTime() { return new Date().format("yyyyMMdd_HHmmss", TimeZone.getTimeZone("Asia/Shanghai")) + "_CN" } def static releaseTime() { return new Date().format("MMddHHmm", TimeZone.getTimeZone("Asia/Shanghai")) }
最新发布
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值