javascript - 如何在使用之前在 JavaScript (ES5) 中强制声明函数构造函数属性?
问题描述
这个问题可能包含答案,我正在考虑可能的解决方案。但是,我想知道,是否(以及如何)有人以更好的方式做到了。
问题是,我有很长的函数构造函数,并且该函数的整个实例包含很多属性,所以我尝试在函数构造函数的开头“初始化”这些属性,因为我想确切地知道,什么被使用并向自己保证,在代码中间的某个地方没有新的属性声明。
我知道,当我们考虑“正常”变量时,当我们“使用严格”时,这很简单:
"use strict"
favouriteAnimal = 'honeybadger'; //Uncaught ReferenceError: favouriteAnimal is not defined
这会抛出错误,因为我们没有使用:
"use strict"
const favouriteAnimal = 'porcupine';
或者
"use strict"
var favouriteAnimal = null //or let
favouriteAnimal = 'aardvark';
在以下这些场景中,请注意“this.importantFunction”和“this.favouriteAnimal”。其余的构造函数只演示了稍微复杂的对象结构,这将受益于这种早期的变量(属性)定义。
但斗争始于对象内部,在以下情况下,这是通常所做的,它正在工作,但它没有提供强制我们首先定义所有变量的安全预防措施。
function TypicalContructorFunction1 ()
{
this.someRandomFunctionWithoutRealPurpose = function () {
console.warn('someone really used me?');
}
this.someStuff = 'coffee break';
this.differentStuff = 'broken coffee';
this.importantFunction = function (animalName) {
this.favouriteAnimal = animalName;
};
this.importantFunction('sewer rat');
return this;
};
var typicalObject1 = new TypicalContructorFunction1();
console.log(typicalObject1.favouriteAnimal)
控制台将记录一个具有属性“favouriteAnimal”的对象,但由于对象结构略胖,因此不清晰可见,因此定义了此类属性。
另一种具有“帮助”功能的方法:
function useProperty (object, propertyName, closure) {
if (object && typeof object[propertyName] !== 'undefined') {
closure(object[propertyName], object, propertyName);
} else {
throw new ReferenceError('property "' + propertyName + '" not declared');
}
};
const TypicalContructorFunction2 = function ()
{
this.someRandomFunctionWithoutRealPurpose = function () {
console.warn('someone really used me?');
}
this.someStuff = 'coffee break';
this.differentStuff = 'broken coffee';
this.importantFunction = function (animalName) {
useProperty(this, 'favouriteAnimal', function (property)
{
property = animalName;
});
};
this.importantFunction('drosophila melanogaster');
return this;
};
var typicalObject2 = new TypicalContructorFunction2();
不需要 console.log,因为会抛出新的 ReferenceError 并停止执行,这正是我想要的。但是,这种调用函数、传递函数来更改简单值似乎有点矫枉过正,我认为它也可能有性能缺陷。
最后(但并非最不重要)我在声明所有属性后想出了密封对象,因此不能添加任何其他属性。(需要“使用严格”)
"use strict"
function TypicalContructorFunction3 ()
{
this.someStuff = null;
this.differentStuff = null;
this.someRandomFunctionWithoutRealPurpose = null;
this.importantFunction = null;
Object.seal(this);
this.someRandomFunctionWithoutRealPurpose = function () {
console.warn('someone really used me?');
}
this.someStuff = 'coffee break';
this.differentStuff = 'broken coffee';
this.importantFunction = function (animalName) {
this.favouriteAnimal = animalName;
};
this.importantFunction('jerboa');
return this;
};
var test = new TypicalContructorFunction3();
ReferenceError:未声明属性“favouriteAnimal”
我认为,这可能是答案,虽然它并没有深深地密封对象(在 StackOverflow 上肯定会有一些带有深度密封的代码片段)。
我也知道,我仍然可以在函数内部使用 var/const/let,但是这些变量会变成私有变量,并且不能从函数外部访问。
你有没有想出不同的解决方案来解决这个问题?
PS:拜托,这个答案是针对ECMAScript v5的,因为它在标题中。 感谢您的想法:)
解决方案
推荐阅读
- javascript - 可以在 php 文件中调试 Javascript 吗?
- javascript - 脚本验证数字 1-100
- android - 在 Andtoid 中了解“通话结束”事件的完美方式
- javascript - 第一次提交按钮单击后,我看不到双向绑定值
- jsoup - JSoup 登录超时
- webdriver-io - Webdriver.io browser.elementIdDisplayed(ID) 不返回布尔值
- r - R数据框:通过对一列进行排序并基于两列中的值来选择行
- c - 在 C 中使用 read() 和 fgets() 的不同执行流程
- python - Python:如何先打印散点图然后删除它
- haskell - 如何在 Haskell 中使用 aio_read(3)?