?? core.md.svn-base
字號(hào):
Core {#Core}============包含一系列在[MooTools](http://mootools.net)中常用的工具函數(shù)。也包含一些諸如[Hash][]和[Array][]的基本擴(kuò)展方法。函數(shù): $chk {#chk}---------------------檢測(cè)參數(shù)值存在(非null, undefined, false, 或 "")或?yàn)?. 適用于將0也視作通行條件的情況.### 語法: $chk(item);### 參數(shù):1. item - (*mixed*) 目標(biāo)檢測(cè)對(duì)象### 返回值:* (*boolean*) 如果傳入的對(duì)象存在或值為0,返回true; 否則返回false.### 示例: function myFunction(arg){ if($chk(arg)) { alert('The object exists or is 0.'); } else { alert('The object is either null, undefined, false, or ""'); } }函數(shù): $clear {#clear}-------------------------清除定時(shí)器(Timeout或Interval). 通常配合[Function:delay][]和[Function:periodical][]方法使用.### 語法: $clear(timer);### 參數(shù):1. timer - (*number*) 要清除的目標(biāo)定時(shí)器, 即由setInterval(periodical)或setTimeout(delay)產(chǎn)生的定時(shí)器對(duì)象引用.### 返回值:* (*false*) 返回null### 示例: var myTimer = myFunction.delay(5000); //等待5秒后執(zhí)行myFunction myTimer = $clear(myTimer); //取消定時(shí)執(zhí)行的myFunction### 另參考:- [Function:delay][]- [Function:periodical][]函數(shù): $defined {#defined}-----------------------------檢測(cè)參數(shù)值是否已定義### 語法: $defined(obj);### 參數(shù):1. obj - (*mixed*) 目標(biāo)檢測(cè)對(duì)象.### 返回值:* (*boolean*) 如果傳入對(duì)象不為null或undefined,返回true; 否則返回false.### 示例: function myFunction(arg){ if($defined(arg)){ alert('The object is defined.'); } else{ alert('The object is null or undefined.'); } }函數(shù): $arguments {#arguments}---------------------------------創(chuàng)建一個(gè)可返回傳入?yún)?shù)的特定項(xiàng)的函數(shù)(詳見示例)### 語法: var argument = $arguments(i);### 參數(shù)1. i - (*number*) 指定返回參數(shù)項(xiàng)的索引號(hào)### 返回值* (*function*) 可返回特定項(xiàng)參數(shù)的函數(shù)### 示例: var secondArgument = $arguments(1); //創(chuàng)建一個(gè)總返回傳入?yún)?shù)的第二個(gè)參數(shù)的函數(shù) alert(secondArgument('a','b','c')); //顯示 "b"函數(shù): $empty {#empty}-------------------------一個(gè)什么事情都不做的空函數(shù). 典型應(yīng)用: 事件監(jiān)聽器的占位方法.### 語法: var emptyFn = $empty;### 示例: var myFunc = $empty; 函數(shù): $lambda {#lambda}-------------------------對(duì)傳入的參數(shù)進(jìn)行函數(shù)封裝.即,如果傳入?yún)?shù)為一個(gè)function,則原樣返回該function; 如果為其他對(duì)象,則返回一個(gè)新創(chuàng)建的function, 該function不做其他任何事,僅僅是返回原來這個(gè)對(duì)象.### 語法: var returnTrue = $lambda(true); ### 參數(shù)1. value - (*mixed*) 將被封裝的對(duì)象### 返回值* (*function*) 一個(gè)能返回給定對(duì)象的函數(shù)### 示例: myLink.addEvent('click', $lambda(false)); //禁止myLink的點(diǎn)擊 //等價(jià)于: myLink.addEvent('click', function(){ return false; }); 函數(shù): $extend {#extend}---------------------------將第二個(gè)參數(shù)對(duì)象的所有屬性復(fù)制到第一個(gè)參數(shù)對(duì)象中.### 語法: $extend(original, extended);### 參數(shù):1. original - (*object*) 目標(biāo)對(duì)象2. extended - (*object*) 源對(duì)象### 返回值:* (*object*) 復(fù)制屬性后的第一個(gè)參數(shù)對(duì)象### 示例:#### 常規(guī)用法: var firstObj = { 'name': 'John', 'lastName': 'Doe' }; var secondObj = { 'age': '20', 'sex': 'male', 'lastName': 'Dorian' }; $extend(firstObj, secondObj); /*firstObj變?yōu)? { 'name': 'John', 'lastName': 'Dorian', 'age': '20', 'sex': 'male' } */#### 特殊用法: myObject.extend = $extend; myObject.extend(otherObject);函數(shù): $merge {#merge}-------------------------合并一組對(duì)象生成新對(duì)象### 語法: var merged = $merge(obj1, obj2[, obj3[, ...]]);### 參數(shù):1. (objects) 任意數(shù)量的對(duì)象### 返回值:* (*object*) 合并生成的新對(duì)象### 示例: var obj1 = {a: 0, b: 1}; var obj2 = {c: 2, d: 3}; var obj3 = {a: 4, d: 5}; var merged = $merge(obj1, obj2, obj3); //返回新對(duì)象: {a: 4, b: 1, c: 2, d: 5} var nestedObj1 = {a: {b: 1, c: 1}}; var nestedObj2 = {a: {b: 2}}; var nested = $merge(nestedObj1, nestedObj2); //返回新對(duì)象: {a: {b: 2, c: 1}}函數(shù): $each {#each}-----------------------迭代數(shù)組(包括非常規(guī)數(shù)組,如由內(nèi)建的getElementsByTagName方法返回的集合對(duì)象, arguments對(duì)象, 或Ojbect對(duì)象)### 語法: $each(iterable, fn[, bind]);### 參數(shù):1. iterable - (*object* or *array*) 被迭代的對(duì)象2. fn - (*function*) 迭代過程中對(duì)每個(gè)迭代元素進(jìn)行處理的函數(shù)3. bind - (*object*, 可選) 迭代函數(shù)中'this'所應(yīng)用的對(duì)象. 詳見 [Function:bind][].#### 參數(shù)詳解: fn##### 語法: fn(item, index, object)##### 參數(shù):1. item - (*mixed*) 當(dāng)前迭代項(xiàng)2. index - (*number*) 當(dāng)前迭代項(xiàng)的索引.如果迭代對(duì)象是一個(gè)Oject對(duì)象, 則是對(duì)象的屬性名3. object - (*mixed*) 迭代對(duì)象### 示例:#### 迭代數(shù)組: $each(['Sun','Mon','Tue'], function(day, index){ alert('name:' + day + ', index: ' + index); }); //顯示 "name: Sun, index: 0", "name: Mon, index: 1" 等...#### 迭代對(duì)象: $each({ first: "Sunday", second: "Monday", third: "Tuesday" }, function(value, key){ alert("The " + key + " day of the week is " + value); }); //顯示 "The first day of the week is Sunday", "The second day of the week is Monday" 等...函數(shù): $pick {#pick}-----------------------返回參數(shù)列表中第一個(gè)非未定義的項(xiàng); 如果全部未定義,則返回null### 語法: var picked = $pick(var1[, var2[, var3[, ...]]]);### 參數(shù):* (*mixed*) 任意個(gè)數(shù)的參數(shù)### 返回值:* (*mixed*) 第一個(gè)非未定義的項(xiàng)* (*false*) 如果都是null或undefined, 則返回null### 示例: function say(infoMessage, errorMessage){ alert($pick(errorMessage, infoMessage, 'There was no message supplied.')); } say(); //顯示 "There was no message supplied." say("This is an info message."); //顯示 "This is an info message." say("This message will be ignored.", "This is the error message."); //顯示 "This is the error message."函數(shù): $random {#random}---------------------------返回指定區(qū)間內(nèi)的一個(gè)隨機(jī)整數(shù)### 語法: var random = $random(min, max);### 參數(shù):1. min - (*number*) 下限(包括)2. max - (*number*) 上限(包括)### 返回值:* (*number*) 給出區(qū)間范圍內(nèi)的一個(gè)隨機(jī)整數(shù)### 示例: alert($random(5, 20)); //顯示: 5~20之間的一個(gè)隨機(jī)整數(shù)函數(shù): $splat {#splat}-------------------------把傳入的參數(shù)包裝成一個(gè)數(shù)組### 語法: var splatted = $splat(obj);### 參數(shù):1. obj - (*mixed*) 任意類型的變量### 返回值:* (*array*) 如果傳入的參數(shù)是個(gè)數(shù)組,那么返回該數(shù)組; 否則,返回一個(gè)包含傳入?yún)?shù)的數(shù)組### 示例: $splat('hello'); //返回 ['hello'] $splat(['a', 'b', 'c']); //原樣返回 ['a', 'b', 'c']函數(shù): $time {#time}-----------------------返回當(dāng)前時(shí)間戳### 語法: var time = $time();### 返回值:* (*number*) - 當(dāng)前時(shí)間戳函數(shù): $try {#try}---------------------嘗試執(zhí)行給出的一組函數(shù), 并返回第一個(gè)執(zhí)行成功的函數(shù)的返回值; 如果一個(gè)都沒執(zhí)行成功,則返回null### 語法: $try(fn[, fn, fn, fn, ...]);### 參數(shù):* fn - (*function*) 將被執(zhí)行的函數(shù)### 返回值:* (*mixed*) 返回第一個(gè)執(zhí)行成功的函數(shù)的返回值* (*null*) 如果一個(gè)都沒執(zhí)行成功,則返回`null`### 示例: var result = $try(function(){ return some.made.up.object; }, function(){ return jibberish.that.doesnt.exists; }, function(){ return false; }); //返回false var failure, success; $try(function(){ some.made.up.object = 'something'; success = true; }, function(){ failure = true; }); if (success) { alert('yey!'); }函數(shù): $type {#type}-----------------------檢測(cè)傳入?yún)?shù)的類型### 語法: $type(obj);### 參數(shù):1. obj - (*object*) 目標(biāo)檢測(cè)對(duì)象### 返回值:* 'element' - (*string*) DOM元素節(jié)點(diǎn)* 'textnode' - (*string*) DOM文本節(jié)點(diǎn)* 'whitespace' - (*string*) DOM空白節(jié)點(diǎn)* 'arguments' - (*string*) arguments* 'array' - (*string*) 數(shù)組* 'object' - (*string*) Object對(duì)象* 'string' - (*string*) 字符串* 'number' - (*string*) 數(shù)字* 'date' - (*string*) 日期對(duì)象* 'boolean' - (*string*) 布爾值* 'function' - (*string*) 函數(shù)對(duì)象* 'regexp' - (*string*) 正則表達(dá)式對(duì)象* 'class' - (*string*) Class (由new Class創(chuàng)建, 或由其他Class對(duì)象擴(kuò)展而來).* 'collection' - (*string*) 原生htmlelements集合, 如由方法childNodes, getElementsByTagName等獲取到的結(jié)果* 'window' - (*string*) window* 'document' - (*string*) document* false - (*boolean*) undefined, null, NaN 或以上列出的類型都不是### 示例: var myString = 'hello'; $type(myString); //返回 "string".[Hash]: /Native/Hash[Array]: /Native/Array[Function:bind]: /Native/Function/#Function:bind[Function:delay]: /Native/Function/#Function:delay[Function:periodical]: /Native/Function/#Function:periodical
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -