隐藏MooTools的
有很多功能,如果你读的源代码可以使用,虽然官方的说法是:if it's not in the documentation, it is not in the api and it's not supported so do not base your code around it as it may change
话虽这么说,有几件事情,真的是非常有用的。我最喜欢的无证特点是:
引用的元素都已经或者正在创建或通过选择传递一个UID
任何元素,被分配的属性uid
,这是渐进的和独特的。由于MooTools 1.4.2,这只能通过Slick.uidOf(node)
而不是通过旧元素attr .uid
读取。您现在可以使用任何MooTools元素对象的新uniqueNumber
属性。
这是如何使用?对于初学者来说,元素存储。它依赖于uid作为关闭中的Storage
对象中的关键字,该关键字将包含任何对该元素具有.store
'd的对象。
element.store('foo', 'bar');
翻译为:
Storage[Slick.uidOf(element)].foo = 'bar';
和
element.retrieve('foo'); // getter of the storage key
element.eliminate('foo'); // delete Storage[Slick.uidOf(element)].foo
初始化存储为您外部创建的元素,例如,通过var foo = document.createElement('div')
而不是元素构造
Slick.uidOf(foo); // makes it compatible with Storage
// same as:
document.id(foo);
钍被存储在由框架入库英格斯还包括所有events
回调,validators
情况下,Fx
实例(吐温,变形等)等。
你能做些什么知道元素的UID?那么,克隆元素不会获取元素的存储或事件。实际上,你可以写一个新的Element.cloneWithStorage
原型,也将复制所有存储的值,你可能有,这是高达一点有用的 - 引用的特定元素(如,Fx.Tween
)情况下,将继续引用旧元素,所以它可能意想不到的结果。这对于移动自己的存储非常有用,但是,您只需要一个类似的方法即可记录您存储的内容并允许您将其克隆。另一种元素的数据
事例存储穿刺:
var foo = new Element('div'),
uid = foo.uniqueNumber;
foo.store('foo', 'foo only');
var bar = new Element('div');
console.log(bar.retrieve('foo')); // null
bar.uniqueNumber = uid; // force overwrite of uid to the other el
console.log(bar.retrieve('foo')); // foo only - OH NOES
console.log(Object.keys(foo)); // ["uniqueNumber"] - oh dear. enumerable!
Function.prototype.protect
也许是一个鲜为人知不错的。
是用于类拥有受保护的方法:
var Foo = new Class({
fooify: function(){
console.log('can\'t touch me');
}.protect(),
barify: function(){
this.fooify();
}
});
var foo = new Foo();
foo.fooify(); // throws error
foo.barify(); // logs "can't touch me"
我个人不经常使用它,但它可能在某些情况下是有用的。
Function.prototype.overloadGetter
和Function.prototype.overloadSetter
看到这个职位:What does MooTools' Function.prototype.overloadSetter() do?
类增变
MooTools有一个奇妙的功能,使您可以创建自己的类存取器。例如,添加一个记录器的被引用特定类的方法,你可以这样做:
// define the mutator as 'Monitor', use as Mointor: ['methodname', 'method2'...]
Class.Mutators.Monitor = function(methods){
if (!this.prototype.initialize) this.implement('initialize', function(){});
return Array.from(methods).concat(this.prototype.Monitor || []);
};
Class.Mutators.initialize = function(initialize){
return function(){
Array.from(this.Monitor).each(function(name){
var original = this[name];
if (original) this[name] = function() {
console.log("[LOG] " + name, "[SCOPE]:", this, "[ARGS]", arguments);
original.apply(this, arguments);
}
}, this);
return initialize.apply(this, arguments);
};
};
,然后在类:
var foo = new Class({
Monitor: 'bar',
initialize: function() {
this.bar("mootools");
},
bar: function(what) {
alert(what);
}
});
var f = new foo();
f.bar.call({hi:"there from a custom scope"}, "scope 2");
尝试的jsfiddle:http://jsfiddle.net/BMsZ7/2/
这个小宝石有对于我在一个HUUUGE异步web应用程序中捕捉嵌套的bugfoot竞争条件问题有帮助,否则这很难追踪。
我建议你阅读的优秀Up the Moo Herd系列Mark Obcena的Pro Javascript With MooTools :)
我知道这不符合所需的格式(每个答案一个功能),但是再次,这个问题并不适合SO格式;) – MattiSG
我最喜欢的功能,后来我才知道,但我希望从一开始就知道作者 - 事件假点,尤其是:once
。
见http://mootools.net/docs/more/Class/Events.Pseudos#Pseudos:once
你可以自由回答任何问题,无论它多大,特别是没有被接受的答案。每个问题都有其访问者,并可能有助于某人。 – Wh1T3h4Ck5
的确是@jdwire,没有限制。当你和我做得更好时,StackOverflow会变得更好。 – artlung
好的。多谢你们。刚加入,所以试图学习我的方式。 –
这应该是一个社区维基 –
我同意。我不知道该怎么做。 – artlung
不幸的是,你需要10k + rep来做到这一点,我想。 –