mootools 获取类名
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event, namespaces are technically just nested objects and I recently shared with you how to . With those utility functions available, I've modified Class so that you can create namespaced classes easily!
由于没有像Dojo Toolkit那样固有地使用和标准化基于命名空间JavaScript类,因此MooTools一直感到有些悲伤。 许多开发人员将他们的类创建为Globals,而这些类通常不被接受。 我大多不同意这种立场,但各持己见。 无论如何,从技术上讲,名称空间只是嵌套对象,我最近与您分享了如何 。 有了这些实用程序功能,我修改了Class,以便您可以轻松创建命名空间的类!
MooTools JavaScript (The MooTools JavaScript)
The technique below is very much like :
以下技术非常类似于 :
(function() { // Keep reference to the original Class var klass = Class; // Redefine class ("that's deep") Class = function(name, params, context) { // Find out if this is namespaced or the original method var namespaced = (arguments.length > 1); // Add the class name to the param list if(namespaced) params.$name = name; // Create and get the original class var original = new klass(namespaced ? params : name); // If namespaced, set class into namespace if(namespaced) Object.place(name, original, context); // Return this newly created class! return original; }; })();
I keep a reference to the original Class
, as I'll be using it within my new Class
function. The new signature allows for a namespaced class name, the usual class properties, and the context which may be passed to the Object.place method. The first step is analyzing the arguments provided to Class; if one argument, it's a traditional class; if more than one argument is provided, we know we're trying to create a namespaced class and will act accordingly. You'll also notice that I add a $name
property to the class prototype, providing the given class name to the class for later use. I've found the class' declaredClass property incredibly helpful within Dojo.
我保留了对原始Class
的引用,因为我将在新的Class
函数中使用它。 新的签名允许使用命名空间的类名称,通常的类属性以及可以传递给Object.place方法的上下文。 第一步是分析提供给Class的参数; 如果有一个论点,那是传统的一类; 如果提供了多个参数,我们知道我们正在尝试创建一个命名空间的类并将采取相应的措施。 您还会注意到,我在类原型中添加了$name
属性,为该类提供了给定的类名称,以供以后使用。 我发现在Dojo中,类的clarifiedClass属性非常有用。
Here's how you'd use the new Class function:
使用新的Class函数的方法如下:
// Create a namespaced classvar myClass = new Class("davidwalsh.ui.Slider", { initialize: function() { // Do stuff! }, doSomething: function(){}});// myClass === davidwalsh.ui.Slider!// Create an instance of the classvar myInstance = new davidwalsh.ui.Slider({ // props});// Get the name of the declared classvar instanceClassName = myInstance.$name; // equals "davidwalsh.ui.Slider"
The first argument becomes the namespace and class name as a string. The second argument is the traditional parameters argument. If only one argument is provided to the Class function, the class is created and returned per usual; in fact, both the namespaced method and traditional method return the newly created class. The method provided above is completely backward compatible so you wont have rewrite your existing code.
第一个参数成为字符串的名称空间和类名称。 第二个参数是传统参数参数。 如果仅向Class函数提供一个参数,则按常规创建并返回该类; 实际上,命名空间方法和传统方法都将返回新创建的类。 上面提供的方法是完全向后兼容的,因此您无需重写现有代码。
Namespaced classes are probably a good idea, as many would argue that "polluting" the global namespace with numerous objects can lead to problems. In any event, this namespacing method should help you make the most of and allow you to keep the global namespace as clean as you'd like!
命名空间类可能是一个好主意,因为许多人认为用大量对象“污染”全局命名空间会导致问题。 无论如何,这种namespacing方法应该可以帮助您充分利用并可以使全局名称空间保持所需的整洁!
翻译自:
mootools 获取类名