mootools类型函数 [英] mootools Type function

查看:107
本文介绍了mootools类型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图通过学习Mootools如何工作内部学习javascript。我特别看这些行:

So I am trying to learn javascript by learning how Mootools works internally. I am looking at these lines specifically:

var Type = this.Type = function(name, object){
    if (name){
        var lower = name.toLowerCase();
        var typeCheck = function(item){
            return (typeOf(item) == lower);
        };

        Type['is' + name] = typeCheck;
        if (object != null){
            object.prototype.$family = (function(){
                return lower;
            }).hide();

        }
    }

    if (object == null) return null;

    object.extend(this);
    object.$constructor = Type;
    object.prototype.$constructor = object;

    return object;
};

//some more code

new Type('Type',Type);

这里发生了什么?

底层的构造函数语句中分配的对象是全局窗口对象?

What is the object being assigned to in the constructor statement down at the bottom, the global window object?

为什么它被称为一个构造函数与新的语句,当Type函数似乎只更新传入的对象而不是创建一个新的?

Why is it being called as a constructor with the new statement when the Type function seems to only update the passed in object rather than creating a new one?

具体什么是'this'在行object.extend(this); ?是全局窗口对象,如果是,则将所有对象的键,值对添加到Type对象中。

Specifically what is 'this' in the line object.extend(this); ? is it the global window object, if so is it adding all the key, value pairs of that object to the Type object?

推荐答案

Sheesh,问题最近似乎更多地集中在mootools内部。

Sheesh, questions lately seem to focus a lot more on mootools internals.

我会回答我最好的知识,因为我不是一个核心开发。 MooTools中的

I will answer to the best of my knowledge as I am not a core dev.

键入与Class非常相似(实际上,类构造函数本身是一个类型),但它更侧重于数据/值和值的类型。它也意味着扩展由ECMA规范定义的本地类型,并使它们更灵活。

Type in MooTools is pretty similar to Class (in fact, the Class constructor itself is a Type) but it focuses more on data / values and the Types of values. It also is meant to extend the native Types defined by ECMA spec and make them more flexible.

我想一般来说,没有必要谈论数据类型( String 数字数组对象等)。为什么还需要扩展它们?那么,对于初学者来说,Duct Typing在js中有点古怪。 typeof {}; // object typeof []; // object typeof new Date(); // object etc - 不是最有用的,即使因为所有类型都继承自对象,并且是逻辑的,他们被分组在一起,它不帮助你编写代码。

I guess there is no point in talking about data types in general (String, Number, Array, Object, etc). Why is there are need to extend them? Well, for starters, Duct Typing is a little quirky in js. typeof {}; // object, typeof []; // object, typeof new Date(); // object etc - not the most helpful, even if since all types inherit from object and is logical for them to be grouped together, it does not help you write code.

因此,在js对象的上下文中,它们是从构造函数对象创建的...

So, in the context of js objects, they are created from constructor objects...

do不是替换构造函数,而是通过向其中添加新的方法或属性来更改现有构造函数。

What Type does is not replace the constructor functions but changes an existing constructor by adding new methods or properties to it.

例如。 new Type('Array',Array);

这会将本机Array构造函数转换为类型对象排序。你不需要保存结果或任何东西 - 这是一个一次性的操作,修改原来的并让它打开操纵。

This will turn the native Array constructor into a type object of sorts. You don't need to save the result or anything - it's a one off operation that mods the original and leaves it open for manipulation.

typeOf (Array); // type

那么,有什么不同?好吧,对于初学者, typeOf([])现在能够实际告诉我们是什么 - array 。这里真正发生的是: object.extend(Type); ,神奇的位。它会将Type对象上定义的所有属性复制到目标对象 - 你可以在这里看到它们:

So, what is different? Well, for starters, typeOf([]) is now able to actually tell us what it really is - array. And what really happens here is this: object.extend(Type);, the magical bit. It will copy to the target object all the properties defined on the Type object - you can see them here:

https://github.com/mootools/mootools-core/blob/master/Source/Core/ Core.js#L211-232

所以,你刚刚创建的Type会得到所有重要的 implements extend 方法。

So, immediately, your newly created Type gets the all important implement and extend methods.

更高级,让我们创建一个基于原生Array构造函数的新类型:

More advanced, let's create a new type that is based on the native Array constructor:

var foo = new Type('foo', Array),
    a = new foo();
// it's a real boy!
a.push('hi');
// ['hi'], foo, object
console.log(a, typeOf(a), typeof a);

但是,如果我们想要一个自定义类型呢?一个是神奇而特别的?没有问题,因为参数2实际上可以是一个(匿名)函数。

But, what if we wanted a custom Type? One that is magical and special? No problem, because argument 2 can actually be a (anonymous) function.

var Awesome = new Type('awesome', function(name, surname) {
    // console.log('I R teh construct0r!'); 
    this.name = name;
    this.surname = surname;
});

// extend it a little.
Awesome.implement({
    getName: function() {
        return this.name;
    },
    getSurname: function() {
        return this.surname;
    }
});

var Dimitar = new Awesome('dimitar', 'christoff');
console.log(typeOf(Dimitar)); // awesome
console.log(Dimitar.getName()); // dimitar

在另一个例子中,看看DOMEvent。它需要上面的,并使其成为更快更精益的对象类型。 https://github.com/mootools/mootools -core / blob / master / Source / Types / DOMEvent.js#L21

In another example, look at DOMEvent. It takes the above and makes it into a faster leaner Object type. https://github.com/mootools/mootools-core/blob/master/Source/Types/DOMEvent.js#L21

那么为什么不是类呢?因为,类更昂贵,事件总是发生。

So why is that not a class? Because, Classes are more expensive and events happen all the time.

我希望这有助于你。对于更详细的解释,请在邮件列表和希望最好的,也许一个核心开发将有时间,因为它不是你的标准我怎么得到手风琴工作类型的问题...

I hope this helps you somewhat. for a more detailed explanation, ask on the mailing list and hope for the best, perhaps a core dev will have time as it's not your standard 'how do I get the accordion working' type of question...

这篇关于mootools类型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆