Javascript 以正确的方式设计一个类
我有一些关于JavaScript的问题需要解决。为了提供帮助,我写了一个简单的类定义:Javascript 以正确的方式设计一个类,javascript,oop,Javascript,Oop,我有一些关于JavaScript的问题需要解决。为了提供帮助,我写了一个简单的类定义: var dataSource = function (src, extension) { return { exists: function () { // function to check if the source exists (src *should* be an object // and extension should b
var dataSource = function (src, extension) {
return {
exists: function () {
// function to check if the source exists (src *should* be an object
// and extension should be a string in the format ".property.property.theSource".
// this function will return true if src.property.property.theSource exists)
},
get: function () {
// function will return the source (ex: return src.property.property.theSource)
}
}
}();
问题:
1) 在我目前对JavaScript的理解中,调用dataSource()将创建一个新对象,其中包含exists()和get()方法的副本。我说得对吗
2) 有没有一种方法可以这样写:如果我创建1000000个数据源对象,我只需要每个函数有一个副本
3) 我是否应该关心(2)?是您想要使用的。它将存储一次并与对象的所有实例关联。您拥有的是一个返回对象的距离的函数,而不是JS类 您将希望使用
DataSource.prototype
进行签出,如果您希望将此与new
结合使用,则应在构造函数中添加属性或修改此
您可能应该这样做:
function DataSource(src, extension){
//Make sure this behaves correctly if someone forgets to use new
if (! this instanceof DataSource)
return new DataSource(src,extension);
//store the constructor arguments
//so you can use them in the shared methods below
this._src=src;
this._extension=extension;
}
DataSource.prototype.exists=function(){
//use this._src and this._extension here
//This method will be available to all
//objects constructed by new DataSource(...)
};
DataSource.prototype.get=function(){
//use this._src and this._extension here
//This method will be available to all
//objects constructed by new DataSource(...)
};
var instance = new DataSource('a source','an extension');
编辑:您提到您更喜欢“私有”变量
构造闭包是模拟私有属性的唯一可移植方法,然而,根据我的经验,在大多数情况下,用
作为前缀,并在组织内部约定不依赖前缀变量就足够了您可以像这样创建类,以便轻松创建多个副本
- 编辑-添加构造函数参数
function DataSource(src, extension) {
this.src = src,
this.extension = extension,
this.exists = function() {
// function to check if the source exists (src *should* be an object
// and extension should be a string in the format ".property.property.theSource".
// this function will return true if src.property.property.theSource exists)
},
this.get = function() {
// function will return the source (ex: return src.property.property.theSource)
}
}
dataSource1 = new DataSource();
dataSource2 = new DataSource();
在返回值中插入了类变量。因此,实例化多少对象,就会创建多少实例
根据您的需求,如果您将类变量与返回类型分开,并且只声明一次(全部),那么对于每个实例,这些属性都是可用的。这意味着这些变量(定义为ExampleClass.prototype=function(){})将作为静态变量在c/c++中工作,并针对ES6类语法进行了更新(这是编写@tobyodavies答案的另一种方式):
不需要检查是否使用new实例调用了该类,因为这在ES6类中是不可能的。它将返回一个类构造函数,如果没有“new”
错误,则无法调用数据源。我仍然可以保持src
和扩展
私有吗?@JustCallmeDrago,你一直在做的事情-构造闭包-是模拟私有变量的唯一可移植方式。根据我的经验,在它们前面加上。
,并且在您的组织内有一个不依赖。
前缀变量的惯例,在大多数情况下就足够了situations@Toby:啊。这就是我要找的。如果你在你的回答中加上这个区别,我会接受的!不要忘记函数赋值后的分号。在某些情况下,它可能会导致解析器产生严重的歧义。ty,总是忘记这些,fixedsrc
和extension
可以保持私有吗?不,我的意思是:“构造闭包-是模拟私有变量的唯一可移植方式…”。我想你会在中编辑它。非常感谢。为什么有人否决了这个答案?我不是Javascript大师,但这看起来是一个完全合法的选择。@tobyodavies这是怎么回事?。我的对象是从一个类创建的,如果需要的话,这个类可以被原型化。问题中创建对象的方式不允许稍后对对象进行原型化,因为没有类。我遗漏了什么?这在问题上是一样的——他想要避免闭包,这一点与new的工作原理无关,它仍然使用闭包,因此没有解决问题的要旨。我同意使用真实类是解决这个问题的必要手段,但这只是为了改变而改变,与没有进一步工作的原始类相比,它没有任何优势。进一步的工作是关于这个问题的,因此它不能单独作为一个答案。@tobyodavies这个关于避免闭包的问题是什么?“有没有一种方法来编写它,这样,如果我创建1000000个数据源对象,我只需要每个函数有一个副本?”-也就是说,没有为每个实例创建闭包。他不要求与新的
(这是唯一不同于OPs代码的方式)兼容,那么这是一个关于其他方面的问题吗?
class DataSource {
constructor(src, extension) {
this._src = src;
this._extension = extension;
}
exists() {
}
get() {
}
};
var instance = new DataSource('a source','an extension');