javascript 插件 - 面向对象?
你好
,
我正在尝试用 javascript 开发一些插件或/和对象,它将控制某些对象的某些属性。它还将使用 jQuery 来简化开发。
这个想法
是用伪代码给你一个想法,因为我不能用正确的单词用英语真正描述它,不可能去使用谷歌来找出我想要使用的东西(并且学习)。
I will have plugin (maybe object?) with some variables and methods:
plugin hideshow(startupconfig){
var c, //collection
add: function(what){
c += what;
},
do: function(){
c.show().hide().stop(); //example code
}
}
我将以这种方式使用它(或排序):
var p = new Plugin();
p
.add($('p#simple'))
.add($('p#simple2'))
.do();
注意
我并不是真的在寻找 jQuery 插件教程 - 它更像是在 javascript 中使用文档中的一个对象,jQuery 是提及只是因为它将用于修改 dom 和简化选择器,jQuery 插件可能只是为了那个小功能 add
。
我正在寻找一些东西,它将位于我的顶部基于计时器或用户操作等从自身记录和调用函数。
问题
- 我真的不知道从哪里开始构建这个对象/插件
- 我不太确定如何维护一个变量,它是 jQuery 对象的集合(类似于
$( '#simple, #simple2');
) - 我可能想用
$.fn.addToPlugin
扩展 jQuery 以使用对象链接,但这应该很简单(实际上只是each( p.add($(this)); )
) - 我最大的问题显然是我无法解释我想要什么。
我希望我能提出任何有意义的想法、链接或建议。
编辑
是这样的,我只是不知道它是如何在 javascript 中调用/使用的 - 伪 PHP 中的示例:
class Foo() {
$collection;
$timer, $action;
function onTimer(){
foreach($collection as $e){
$e->someAction();
}
}
function add($e){
$collection[] = $e;
}
function start(){
$timer->start( onTimer() );
}
function->stop(){
$timer->stop();
}
}
var f = new Foo();
Foo.add(something);
Foo.start();
intro
Hello,
I'm trying to develop some plugin or/and object in javascript, which will control some properties over some object. It will also use jQuery, to ease development.
idea
This is in pseudocode to give you an idea, since I can't really describe it in english with the right words, it's impossible to go and use google to find out what I want to use (and learn).
I will have plugin (maybe object?) with some variables and methods:
plugin hideshow(startupconfig){
var c, //collection
add: function(what){
c += what;
},
do: function(){
c.show().hide().stop(); //example code
}
}
and I will use it this way (or sort-of):
var p = new Plugin();
p
.add($('p#simple'))
.add($('p#simple2'))
.do();
note
I'm not really looking for jQuery plugin tutorials - it's more like usage of one object in document in javascript, jQuery is mentione only because it will be used to modify dom and simplify selectors, jQuery plugin maybe just for that one little function add
.
I'm looking for something, that will sit on top of my document and call functions from itselft based on timer, or user actions, or whatever.
problems
- I don't really know where to start with construction of this object/plugin
- I'm not really sure how to maintain one variable, which is collection of jQuery objects (something like
$('#simple, #simple2');
) - I would maybe like to extedn jQuery with
$.fn.addToPlugin
to use chaining of objects, but that should be simple (really justeach( p.add($(this)); )
) - My biggest problem is obviously that i can't explain what I want.
I hope I make any sense, ideas, links or advices appreciated.
edit
It's something like this, I just can't figure out how it's called/used in javascript - example in pseudo PHP:
class Foo() {
$collection;
$timer, $action;
function onTimer(){
foreach($collection as $e){
$e->someAction();
}
}
function add($e){
$collection[] = $e;
}
function start(){
$timer->start( onTimer() );
}
function->stop(){
$timer->stop();
}
}
var f = new Foo();
Foo.add(something);
Foo.start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看插件/Authoring。我想说的是,您提出的 API 并不是真正的“jQuery 方式”。更有可能的是:
起点是:
你基本上就完成了。
现在调用这一系列事件也没有任何意义,但这是另一个问题。
现在,方法链接变得更有趣了,我想这就是“单例”的用武之地。现在在 jQuery 的情况下——至少对于 jQuery 插件——状态作为一般规则存储在 jQuery 对象本身上:
并由插件访问:
然后允许:
现在这有一个限制,即能够将参数传递给这些效果。不太确定如何解决这个问题。
Take a look at Plugins/Authoring. I will say that your proposed API isn't really the "jQuery way". The would more likely be:
A starting point would be:
and you're basically done.
Now calling this sequence of events doesn't really make sense either but that's another issue.
Now the method chaining is where it gets a little more interesting and I guess this is where the "singleton" comes in. Now in jQuery's case--at least for jQuery plugins--state is stored on the jQuery object itself as a general rule:
and accessed by the plugin:
which then allows:
Now this has a limitation of being able to pass arguments to those effects. Not quite sure how to get around that.
如果您想用 Javascript 编写单例,请使用 YAHOO 模块模式 应该向您展示您需要的一切。
If you are looking to write a singleton in Javascript, the YAHOO module pattern should show you everything you need.