One week learn Mootools 1.4 Chinese tutorial :( 4) Type

Source: Internet
Author: User
Tags mootools

The types of Mootools mainly include the following parts: String; Number: Number; Array: Array; Object: Object; Json:; Cookie :.

This is also the focus of our speech today. each data type Mt has extended its own method, which can be seen through the Editplus Material file I provided for you, because there are many methods involved, so I will select several common methods to explain to you. For other methods not mentioned, please refer to the instructions and examples in the material file.
The usage of this part is very simple. You only need to know what data type you are operating on, after knowing the data type, you need to know the methods that mt provides for the operations of this type of data. If you cannot confirm what type of data you are operating on, you only need to know this sentence.

Alert (typeOf (???));

String:

// From converts the input parameter into a string, for example:
String. from (2); // returns '2'
String. from (true); // returns 'true'

// UniqueID generates a unique ID
Alert (String. uniqueID ())

// Trim clears the blank strings at both ends of the string
Alert ('I like cookies'. trim ())

// ToInt converts a string to a number and converts the string to a base value.
Alert ('4em '. toInt (); // returns 4
'10px '. toInt (); // returns 10

// ToFloat converts a string to a floating point number.
Alert ('95. 25% '. toFloat (); // returns 95.25
'10. 848 '. toFloat (); // returns 10.848

// ToLowerCase/toUpperCase case Conversion
Alert ('A'. toLowerCase ())

// Test match a string using a regular expression
Alert ('I like cookies'. test ('cooker', 'I'); checks whether the string contains the specified substring in case insensitive.

// EscapeRegExp escapes characters sensitive to regular expressions in strings
Alert ('animals. sheep [1] '. escapeRegExp (); // returns 'animals \. sheep \ [1 \]'

// Replace tags in a template similar to substitute
Var my1 = '{a} | {B} | {B _2 }.';
Var my2 = {a: 'This is tag a,', B: 'This is tag B', B2. what about this? '};
My1.substitute (my2 );

The nine methods above are commonly used to operate String-type data. You can take a closer look at each purpose and examples. I believe it is not difficult to understand them.

Number:

// From converts the input parameter to Number
Alert (Number. from ('12'); // returns 12
Number. from ('hello') // returns null

// Random number returned by random
Number. random (5, 20); // returns a random number between 5 and 20.

// Round
(12.45). round () // returns 12
(12.45). round (1) // returns 12.5
(12.45). round (-1) // returns 10

// ToInt converts a number into a number and converts it in the base hexadecimal format given.
(111). toInt (); // returns 111
(111.1). toInt (); // returns 111
(111). toInt (2); // returns 7

// ToFloat converts a number to a floating point number
(111). toFloat (); // returns 111
(111.1). toFloat (); // returns 111.1

// Common mathematical methods
/* Abs
Acos
Asin
Atan2
Ceil
Cos
Exp
Floor
Log
Max
Min
Pow
Sin
Sqrt
Tan
*/
(-1). abs (); // returns 1
(3). pow (4); // returns 81


Array:

// Common Array format
Var arr = [1, 2, 4];
Var arr = new Array (1, 2, 3, 4 );

// Array. each looping Array
Var arr = ['sun', 'mon', 'tue '];
Array. each (arr, function (day, index ){
Alert ('name: '+ day +', index: '+ index );
});

// Every returns true if each item in the array passes the test of the given function.
Var arr = [25,100,];
Var areAllBigEnough = arr. every (function (item, index ){
Return item> 20;
});

// Clean creates a new array based on the existing array. Each member must have been defined, and IE browser must be non-null and non-undefined
Var myArray = [null, 1, 0, true, false, 'foo', undefined, ''];
MyArray. clean () // returns [, true, false, 'foo', '']; // false is also valid because it has been defined

// IndexOf returns the index number of the items in the array that are equal to the given parameter value. If no equal item is found,-1 is returned.
['Apple', 'limon', 'bana']. indexOf ('limon'); // returns 1
['Apple', 'limon']. indexOf ('banana '); // returns-1

// Map calls an external function to process every item in the existing array and generate a new array.
Var timesTwo = [1, 2, 3]. map (function (item, index ){
Return item * 2;
}); // TimesTwo = [2, 4, 6];

// Some if at least one item in the array passes the test of the given function, true is returned.
Var isAnyBigEnough = [25,100,]. some (function (item, index ){
Return item> 20;
}); // IsAnyBigEnough = true

// Flatten converts a multi-dimensional array to a one-dimensional array
Var myArray = [, 3, [, [6, 7], [[8];
Var newArray = myArray. flatten (); // newArray is [1, 2, 4, 5, 6, 7, 8]

// Empty clear the Array
Var myArray = ['old', 'data'];
MyArray. empty (); // myArray is now []

// Erase removes a project from the array
['Cow', 'pig', 'Dog', 'cat', 'Dog']. erase ('Dog') // returns ['cow', 'pig', 'cat']
['Cow', 'pig', 'Dog']. erase ('cat') // returns ['cow', 'pig', 'Dog']

// GetRandom randomly extracts key values from the array
['Cow', 'pig', 'Dog', 'cat']. getRandom (); // returns one of the items

// Append merges the array and appends the new array to the end.
Var mytherarray = ['green', 'yellow'];
['Red', 'blue']. append (mytherarray); // returns ['red', 'blue', 'green', 'yellow'];
MyOtheArray; // is now ['red', 'blue', 'green', 'yellow'];
[0, 1, 2]. append ([3, [4]); // [0, 1, 2, 3, [4]

// Contains test whether the specified item exists in the array
['A', 'B', 'C']. contains ('A'); // returns true
['A', 'B', 'C']. contains ('D'); // returns false

Object:

// Common Object formats
Var O = {a: 0, B: 1 };

// Each Traversal
Var O = {first: 'sunday', second: 'monday', third: 'tuesday '};
Object. each (O, function (value, key ){
Alert ('the '+ key + 'Day of The week is' + value );
});

// Merge merges a group of Obj to generate a new Obj.
Var obj1 = {a: 0, B: 1 };
Var obj2 = {c: 2, d: 3 };
Var obj3 = {a: 4, d: 5 };
Var merged = Object. merge (obj1, obj2, obj3); // returns {a: 4, B: 1, c: 2, d: 5}, (obj2, and obj3 are unaltered)
Merged === obj1; // true, obj1 gets altered and returned as merged object
Var nestedObj1 = {a: {B: 1, c: 1 }};
Var nestedObj2 = {a: {B: 2 }};
Var nested = Object. merge (nestedObj1, nestedObj2); // returns: {a: {B: 2, c: 1 }}

// Append merges Obj and appends it to the end.
Var firstObj = {
Name: 'john ',
LastName: 'doe'
};
Var secondObj = {
Age: '20 ',
Sex: 'male ',
LastName: 'dorian'
};
Object. append (firstObj, secondObj );
// FirstObj is now: {name: 'john', lastName: 'dorian ', age: '20', sex: 'male '};

// Subset obtains the subset from Obj.
Var object = {
A: 'one ',
B: 'two ',
C: 'Three'
};
Object. subset (object, ['A', 'C']); // returns {a: 'one', c: 'Three '}

// Map calls an external function to process every item in the existing Obj and generate a new Obj
Var myObject = {a: 1, B: 2, c: 3 };
Var timesTwo = Object. map (myObject, function (value, key ){
Return value * 2;
}); // TimesTwo now holds an object containing: {a: 2, B: 4, c: 6 };

// Keys returns all keys in Obj as an array
Var myObject = {e: 10, B: 4, c: 25, d: 100 };
Var keys = Object. keys (myObject );
Alert (keys)

// Values returns all values in Obj as an array.
Var myObject = {e: 10, B: 4, c: 25, d: 100 };
Var keys = Object. values (myObject );
Alert (keys)

// GetLength returns the number of Obj Elements
Var myObject = {
Name: 'john ',
LastName: 'doe'
});
Object. getLength (myObject); // returns 2

// KeyOf queries the name of the key in Obj based on the value. If no equal item is found, false is returned.
Var myObject = {a: 'one', B: 'two', c: 3 };
Object. keyOf (myObject, 'two'); // returns 'B'
Object. keyOf (myObject, 3); // returns 'C'
Object. keyOf (myObject, 'four'); // returns false

// Contains test whether a value exists in Obj.
Var myObject = {a: 'one', B: 'two', c: 'Three '};
Object. contains (myObject, 'one'); // returns true
Object. contains (myObject, 'four '); // returns false

// ToQueryString converts a project in Obj to a Url address, and then URIencode
Object. toQueryString ({apple: 'red', lemon: 'yellow'}); // returns 'apple = red & lemon = yellow'
Object. toQueryString ({apple: 'red', lemon: 'yellow'}); // apple = % E7 % BA % A2 % E8 % 89% B2 & lemon = yellow
Object. toQueryString ({apple: 'red', lemon: 'yellow'}, 'fruits '); // returns 'fruits [apple] = red & fruits [lemon] = yellow'

JSON:

// Common Json format
Var J = {"_ 5": "Stop", "_ 4": "W \ I", "_ 3": "Success \/g "};

// Encode converts Obj or array to Json
Var fruitsJSON = JSON. encode ({apple: 'red', lemon: 'yellow'}); // returns: '{"apple": "red", "lemon": "yellow "}'

// Decode converts a Json object to Obj.
Var myObject = JSON. decode ('{"apple": "red", "lemon": "yellow"}'); // returns: {apple: 'red', lemon: 'yellow '}

Cookie:

// Write
Var myCookie = Cookie. write ('username', 'jackbauer ');

// Valid for one day:
Var myCookie = Cookie. write ('username', 'jackbauer ', {duration: 1 });

// Read
Cookie. read ('username ');

// Dispose destroys a cookie
Cookie. dispose ('username'); // Bye-bye JackBauer!

// The best way to write
Var myCookie = Cookie. write ('username', 'jackbauer ', {duration: 1, domain: 'mootools. net '});
If (Cookie. read ('username') = 'jackbauer ') {myCookie. dispose ();}

This course has many contents. Please practice more.

(Source: http://see7di.cnblogs.com)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.