mootools架構【二】-Core篇:方法完全解析

來源:互聯網
上載者:User

說明: Core 類中的核心庫是Moo.js和Utility.js,Moo.js裡包含了實現OO的基礎類,Utility.js提供了最常用的工具函數的封裝。

---------------------------Moo.js-------------------------------------

1.mootools使用如下形式構建對象:

var Cat  = new Class({
initialize: function(name){
this.name = name;
}
});
var cat = new Cat ('kitty');
alert(cat.name);

2.Class進行繼承,mootools是這樣做的:

var Animal = new Class({
initialize: function(name){
this.name = name;
}
});
var Cat = Animal.extend({
initialize: function(name,age){
this.parent(name); //調用Animal的initialize方法
this.age = age;
}
});

3.Class.implement方法對已經定義好的類進行擴充

(例如添加方法),用法如下:

var Person = new Class({
initialize: function(name){
this.name = name;
}
});
Person.implement({
initialize: function(name,age){
this.name = name;
this.age = age;
},
myAction: function(){
alert(this.name+'--'+this.age);
}
});
var p = new Person('zarknight',24);
p.myAction();

mootools對Object擴充了一個extend方法,它有兩個參數,作用是複製第二個參數對象的所有屬性到第一參數對象中去,同名屬性將覆蓋為第二個參數中的相應屬性:

var firstOb = {
'name': 'John',
'lastName': 'Doe'
};
var secondOb = {
'age': '20',
'sex': 'male',
'lastName': 'Dorian'
};
Object.extend(firstOb, secondOb);
//firstOb將變成:
{
'name': 'John',
'lastName': 'Dorian',
'age': '20',
'sex': 'male'
};

---------------------------Moo.js-------------------------------------

4.$type

作用:基本上是對typeof的擴充,返回輸入參數的類型

例子:

var x=$type("abcdefg");

傳回值:

'element'-DOM
'textnode'-DOM文本節點
'whitespace'-DOM空白節點
'array'-數組
'object'-對象
'string'-字串
 'number'-數字
 'boolean'-布爾型
'function'-函數

false-如果是未定義或則不是以上列出的類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.