admin管理员组文章数量:1389768
I have been reading up on Mixins using Coffeescript or just plain Javascript from the following sources:
.html (near the bottom)
and
/
And while I am able to pile the various examples, I have a major question that seems to be preventing me from making progress in prehending them.
I have no idea what in the world is going on. To start, I will explain the Coffeescript that is confusing me.
moduleKeywords = ['extended', 'included']
class Module
@extend: (obj) ->
for key, value of obj when key not in moduleKeywords
@[key] = value
obj.extended?.apply(@)
this
@include: (obj) ->
for key, value of obj when key not in moduleKeywords
# Assign properties to the prototype
@::[key] = value
obj.included?.apply(@)
this
A number of questions e up here.
First of all, what are we acplishing with the
moduleKeywords
variable? I'm not understanding what that is doing.Secondly, how does
extended?.apply(@)
work? What is really going on here? I can look at the JavaScript pilation and see the following code ..
Module.extend = function(obj) {
var key, value, _ref;
for (key in obj) {
value = obj[key];
if (__indexOf.call(moduleKeywords, key) < 0) {
this[key] = value;
}
}
if ((_ref = obj.extended) != null) {
_ref.apply(this);
}
return this;
};
Can anyone shed some general light on this?
From deeper down in The Little Book on Coffeescript
, I see an implementation.
ORM =
find: (id) ->
create: (attrs) ->
extended: ->
@include
save: ->
class User extends Module
@extend ORM
Here is how I read this:
- create literal
ORM
. - Declare method
find
accepting a parameter. - Declare method 'create' accepting a parameter.
- Declare method 'extended', with sub-method 'include', with sub-method 'save'.
This is where I get the most lost.
The literal ORM
has a method, extended
, and then Module
is implemented/extended by the 'class' User
. So I take this to mean that User
has the same shape as Module
. That part makes sense so far, simplistic inheritance. But then I get lost on @extend ORM
.
@extend
is a method on Module
, but what is the extended
method doing? When is it called? How is it implemented?
I have been reading up on Mixins using Coffeescript or just plain Javascript from the following sources:
http://arcturo.github./library/coffeescript/03_classes.html (near the bottom)
and
http://javascriptweblog.wordpress./2011/05/31/a-fresh-look-at-javascript-mixins/
And while I am able to pile the various examples, I have a major question that seems to be preventing me from making progress in prehending them.
I have no idea what in the world is going on. To start, I will explain the Coffeescript that is confusing me.
moduleKeywords = ['extended', 'included']
class Module
@extend: (obj) ->
for key, value of obj when key not in moduleKeywords
@[key] = value
obj.extended?.apply(@)
this
@include: (obj) ->
for key, value of obj when key not in moduleKeywords
# Assign properties to the prototype
@::[key] = value
obj.included?.apply(@)
this
A number of questions e up here.
First of all, what are we acplishing with the
moduleKeywords
variable? I'm not understanding what that is doing.Secondly, how does
extended?.apply(@)
work? What is really going on here? I can look at the JavaScript pilation and see the following code ..
Module.extend = function(obj) {
var key, value, _ref;
for (key in obj) {
value = obj[key];
if (__indexOf.call(moduleKeywords, key) < 0) {
this[key] = value;
}
}
if ((_ref = obj.extended) != null) {
_ref.apply(this);
}
return this;
};
Can anyone shed some general light on this?
From deeper down in The Little Book on Coffeescript
, I see an implementation.
ORM =
find: (id) ->
create: (attrs) ->
extended: ->
@include
save: ->
class User extends Module
@extend ORM
Here is how I read this:
- create literal
ORM
. - Declare method
find
accepting a parameter. - Declare method 'create' accepting a parameter.
- Declare method 'extended', with sub-method 'include', with sub-method 'save'.
This is where I get the most lost.
The literal ORM
has a method, extended
, and then Module
is implemented/extended by the 'class' User
. So I take this to mean that User
has the same shape as Module
. That part makes sense so far, simplistic inheritance. But then I get lost on @extend ORM
.
@extend
is a method on Module
, but what is the extended
method doing? When is it called? How is it implemented?
2 Answers
Reset to default 5extend
copies the methods from the "module" object onto the object being extendedinclude
copies the methods from the "module" object onto the prototype of the object being extended
1 The moduleKeywords
is used to protect some methods of the module, so the are not copied to object, because they have special meaning
2 The extended?.apply(@)
says that if the module has a property named extended
than assume it's a function and then call this function having the "this" in the function equal to @, @ being the extended object, you can think of it as saying something like (although not quite, but it's just an intuition) @.extended()
(@ == this in coffeescript)
"apply" function in JS
the existential operator in CS
You are confused by meaning and use for extended and included Module keywords. But it is explained in book that those are used as callbacks after extending and including.
So in final example ORM has "extended" callback. The "extend" function will on end call "extended" and pass it @ (or this or in our example User) so @(this.)include will also run on User and it will include function "save".
You could also do the reverse:
ORM =
save ->
included: ->
@extend
find: (id) ->
create: (attrs) ->
class User extends Module
@include ORM
本文标签: How to better understand CoffeescriptJavaScript mixinsStack Overflow
版权声明:本文标题:How to better understand CoffeescriptJavaScript mixins? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744722652a2621788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论