Javascript 从构造函数实例化的类范围对象
对于下面的someAPI,它需要我希望在构造函数中动态分配的凭据。然后我想在整个课程中使用一些API。也就是说,在下面的示例中,someMethodUsingSomeAPI是我想从B实例中的其他方法调用的一个助手方法。使用Coffee-/JavaScript可以吗?(我能让它工作的唯一方法是在构造函数中使用someMethodUsingSomeAPI。) 根据saimeunt的建议更新Javascript 从构造函数实例化的类范围对象,javascript,oop,design-patterns,meteor,coffeescript,Javascript,Oop,Design Patterns,Meteor,Coffeescript,对于下面的someAPI,它需要我希望在构造函数中动态分配的凭据。然后我想在整个课程中使用一些API。也就是说,在下面的示例中,someMethodUsingSomeAPI是我想从B实例中的其他方法调用的一个助手方法。使用Coffee-/JavaScript可以吗?(我能让它工作的唯一方法是在构造函数中使用someMethodUsingSomeAPI。) 根据saimeunt的建议更新 ... someMethodUsingSomeAPI = (id) -> wrappedGet =
...
someMethodUsingSomeAPI = (id) ->
wrappedGet = Async.wrap(@someAPI, 'get')
wrappedGet 'whatever/show', { id: id }
console.log someMethodUsingSomeAPI '123' # ReferenceError: someMethodUsingSomeAPI is not defined
&
将someMethodUsingSomeAPI:
更改为someMethodUsingSomeAPI=
console.log someMethodUsingSomeAPI '123' # Error: unsupported argument list
&
b=b('username'))
b、 someMethodUsingSomeAPI“123”#类型错误:对象#没有方法“someMethodUsingSomeAPI”
(这是流星0.9.3.1)
更新以试图澄清
,
我很高兴使用:,但我真的希望它能在实际实例中工作。当然,您可以将
someAPI
附加到类对象上。在coffeescript中,@
用于代替此
(就像在Javscript中一样)
您可以看看是什么解释了Javascript
this
,以及它的范围。一旦你理解了这一点,看看它是如何工作的,你应该对@
的使用有一些清晰的认识,我想这就是你想要的:
someMethodUsingSomeAPI:function(id){
var wrappedGet=Async.wrap(@someAPI,"get");
return wrappedGet("whatever/show",{
id:id
});
}
根据您提供的简化示例,这里的版本应符合您的要求:
class SomeClass
# Store the last created instance at the class level
lastInstance: null
constructor: (options = {}) ->
@someLastName = options.someLastName
# In constructor, the created instance is now the lastInstance
SomeClass.lastInstance = this
# The helper method is not created in the class scope
someMethod = (someFirstName) ->
"Hello " + someFirstName + " " + SomeClass.lastInstance.someLastName
someClass = new SomeClass({ someLastName: 'Borgnine' })
alert someMethod 'Ernest'
和
基本上,当您希望助手方法访问类的实例时,您需要提供对该实例的引用,助手可以在某个时候引用该实例。在这里,我使用一个类变量来存储最后创建的实例,并且助手在调用时访问最后一个实例。这意味着在创建多个实例时,它将执行以下操作:
someClass = new SomeClass({ someLastName: 'Borgnine' })
alert someMethod 'Ernest' # Hello Ernest Borgnine
someClass = new SomeClass({ someLastName: 'Doe' })
alert someMethod 'Boris' # Hello Boris Doe
为什么要将
someAPI
设置为类外的静态变量,而不是实例属性?请注意,JSON.parse
采用的是JSON字符串,而不是文件路径。是的,对此表示抱歉。为了简洁起见,我去掉了一些。添加回来。您使用someMethodUsingSomeAPI=
在它应该位于someMethodUsingSomeAPI:
的位置。然后,实例化aB
,并对其调用方法。或者你为什么要定义一个类?如果我将它从=改为:,我会得到ReferenceError:someMethodUsingSomeAPI没有用上面的跟踪定义。如果我使用someAPI“123”执行B.someMethod
I get无法调用未定义的方法“get”。我想从B中的其他方法调用这个helper方法。我想在尝试在构造函数之外声明someAPI之前,我首先尝试了这一点。更新了原始帖子以反映我正在做的事情,但我仍然得到相同的错误,即,无法调用未定义的
的方法“get”。这对b.someMethodUsingSomeAPI非常有效,但是除非我把CS搞糟了,否则它在实例中是不起作用的。使用结果更新了上面的示例。在定义对象文字的新属性时,请删除=
,以支持:
,因为如果您不这样做,则完全没有意义,我不知道CS到底是如何不警告您的。没有new
,B的这些实例怎么样?您是否使用其他方法中的正确语法调用了someMethodUsingSomeAPI,即@someMethodUsingSomeAPI
?好的。我不知道为什么,但是如果我将这两种方法都放在构造函数中,那么使用=我实际上有console.log someMethodUsingSomeAPI“123”
可以工作。B新的似乎没有什么不同。只返回一个新的,除非已经是构造函数中声明的B的实例。如果我执行console.log@someMethodUsingSomeAPI'123'
,我会得到TypeError:Object\35;没有方法'someMethodUsingSomeAPI'
,您需要编辑代码并删除省略号,以向我们展示您实际测试的对象。告诉我们console.log的结果没有什么帮助,因为我们不知道执行的上下文。这是完整的代码,我不确定我能不能比介绍中更简洁地描述这个问题。同样地,someMethodUsingSomeAPI
在从同一级别方法调用时工作,如果两者都在构造函数中,(@)someAPI
似乎必须同时声明和实例化才能使用这些动态凭据someMethodUsingSomeAPI
如果不在构造函数中,则从同一级别方法调用时不起作用,因为(@)someAPI未定义,或者如果在构造函数外部声明,则没有get方法。
SomeAPI = require 'someAPI'
class A extends B
constructor: (options = {}) ->
unless @ instanceof A
return new A(options)
@config = JSON.parse('config/' + options.username + '.json')
@someAPI = new SomeAPI
consumer_key: @config.credentials.consumer.key
consumer_secret: @config.credentials.consumer.secret
access_token: @config.credentials.access.token
access_token_secret: @config.credentials.access.secret
someMethodUsingSomeAPI: (id, callback) ->
return @someAPI.get 'whatever/show', { 'id': id }, callback
someMethodUsingSomeAPI:function(id){
var wrappedGet=Async.wrap(@someAPI,"get");
return wrappedGet("whatever/show",{
id:id
});
}
class SomeClass
# Store the last created instance at the class level
lastInstance: null
constructor: (options = {}) ->
@someLastName = options.someLastName
# In constructor, the created instance is now the lastInstance
SomeClass.lastInstance = this
# The helper method is not created in the class scope
someMethod = (someFirstName) ->
"Hello " + someFirstName + " " + SomeClass.lastInstance.someLastName
someClass = new SomeClass({ someLastName: 'Borgnine' })
alert someMethod 'Ernest'
someClass = new SomeClass({ someLastName: 'Borgnine' })
alert someMethod 'Ernest' # Hello Ernest Borgnine
someClass = new SomeClass({ someLastName: 'Doe' })
alert someMethod 'Boris' # Hello Boris Doe