Microsoft Ajax library extensions to JavaScript native types

Source: Internet
Author: User

What is Microsoft Ajax library?
· Client section of ASP. NET Ajax
· Client-only framework (independent from the server)
· ProvidedJS ExtensionAndBasic Library

Common native types in javascript:
· Object
· Arrary
· Error
· Function
·.....

Object native type
· An unordered set that can store any type of Objects
· It is often used as a dictionary. The value assignment uses OBJ ["itemname"] Or obj. itemname, where key is a string type and value is of any type.
In addition, you can use for-in to traverse each item in the dictionary.
· Extension of the object's prototype object is prohibited, because if the object's prototype object is extended, its extension will appear in all objects
Response to the for operation (Microsoft Ajax library does not have an extension object)

[Code demo ]:
VaR OBJ = new object ();
OBJ ["name"] = "dongde ";
OBJ. Salary = 10000;
Alert (OBJ ["name"] + "Salary:" + obj. Salary );

[Code demo ]:
VaR dict = new object ();
For (VAR I = 0; I <10; I ++)
{
Var key = "Key _" + I;
Dict [Key] = math. Random ();
}

Delete dict ["key_8"];

For (var key in dict)
{
Alert ("key:" + key + ", value +" + dict [Key]);
}

Array native type
· A variable-length array object with a maximum length of 2 ^ 32-1
· Create arrary
New array ();
New array (3 );
New array (9, "dongde ");
· Common methods:
Concat returns a new array, saving all elements and parameters of the original array.
Push adds one or more elements to the end of the array.
Pop removes and returns elements from the end of the array.
Shift removes and returns elements from the array Header
Unshift adds one or more elements to the array Header
Join (sepatator) returns the string of all elements linked with the separator sepatator.
Reverse reverse Array
Sort (comparefunction) compares two elements, which can be used to sort numeric and string elements. For other types, comparefunction parameters must be provided.
Slice (START, end) returns a new array, including starting from subscript to end. If there is no end, it returns a new element from start to the end.
Array. If start is before end or is the same, an empty array is returned. If the end parameter is negative, it indicates that the subscripts of the last few end.

Splice splice (2, 1) deletes an element (an element is deleted starting from subscript 2)
Splice (2.0, "AAA", "BBB") inserts an element (from subscript 2)
Splice (2, 1, "AAA", "BBB") replacement element

· Array native type Extension
Array. enqueue (array, item); in the queue, add item to the end of Array
Array. dequeue (array); out of the queue, delete and return the first element of Array
Array. addrange (array, items); Add the left and right elements in the items array to the end of the array.
Array. Contains (array, item); returns true if an element exists and false if not
Array. Clear (array); clear all elements in array
Array. insert (array, index, item );
Array. Remove (array, item); remove the item Element
Array. removeat (array, index );
Array. Clone (array); returns a new array that is the same as array.
Array. parse (value );
Array. indexof (array, item, start); search for the item element from start and return the subscript of the item. If there is no item element, return-1.
Array. Add (array, item); the same effect as array. enqueue (array, item)
Array. foreach (array, method, instance );

Error native type
· Indicates the error object

 

Function native type

· Every method is a function-type instance.
Typeof (array) = typeof (function) = "function"
· Determine the context of this based on the initiated object during method calling
· Function. Call (instance, [arg1 [, arg2 [,...]) and function. Apply (instance, argS)
Each function has two methods: Call and apply. The context reference of this when the instance is used as the method call.
The args in the apply method indicates that all the elements in the ARGs are input
[Arg1 [, arg2 [,...] in the call method can be written in sequence.

[Code demo]:
function show (content)
{< br>
document. getelementbyid ("divid "). innerhtml + = (content + "
");
}

function testmethod ()
{< br> var result = This. tostring ();
for (VAR I = 0; I {< br> result + = ("," + arguments [I]);

}< br> return result;
}

VaR Mary = new string ("My name is Mary ");
VaR lin = new string ("My name is Lin ");
Mary. testmethod = testmethod;
Lin. testmethod = testmethod;

Show (testmethod (); // output [object]
Show (Mary. testmethod (); // output my name is Mary this pointer pointing to Mary
Show (Lin. testmethod (); // output my name is Lin this pointer pointing to Lin

Show ("mary. amethod. call (B, 1, 2, 3): "+ Mary. amethod. call (Lin, 1, 2, 3); // output my name is Lin, 1, 2, 3 this pointer pointing to Lin
Show ("Lin. amethod. apply (A, [1, 2, 3]): "+ lin. amethod. apply (Mary, [1, 2, 3]); // output my name is Mary, 1, 2, 3 this pointer pointing to Mary

VaR cc = new string ("My name is cc ");
Show ("mary. amethod. call (CC, 1, 2, 3): "+ Mary. amethod. call (CC, 1, 2, 3); // output my name is CC, 1, 2, 3 this pointer pointing to CC


· Function native type Extension

Function. createdelegate (instance, method); To get a reference to a method, the method is called when the method is executed, and the context of the method this points to the instance

Function. createcallback (method, context); obtain a method reference, execute the method, and pass context as an additional parameter.

[Code demo ]:

<Asp: scriptmanager id = "scriptmanager1" runat = "server"/>
<Input type = "button" value = "Click me" id = "BTN"/>

<Script language = "JavaScript" type = "text/JavaScript">

VaR oo = {

Textcontext: "dongde ",
Onclick: function (E)
{
Alert ("Hello:" + this. textcontext );
}

}
OO. onclick ();

</SCRIPT>

// Output: dongde

 

<Asp: scriptmanager id = "scriptmanager1" runat = "server"/>
<Input type = "button" value = "Click me" id = "BTN"/>

<Script language = "JavaScript" type = "text/JavaScript">

VaR oo = {

Textcontext: "dongde ",
Onclick: function (E)
{
Alert ("Hello:" + this. textcontext );
}

}

$ Addhandler ($ get ("BTN"), "click", oo. onclick); // events that add DOM elements across browsers

</SCRIPT>

// Output undefined

 

<Asp: scriptmanager id = "scriptmanager1" runat = "server"/>
<Input type = "button" value = "Click me" id = "BTN"/>

<Script language = "JavaScript" type = "text/JavaScript">

VaR oo = {

Textcontext: "dongde ",
Onclick: function (E)
{
Alert ("Hello:" + this. textcontext );
}

}

// $ Addhandler ($ get ("BTN"), "click", oo. onclick); // events that add DOM elements across browsers

VaR onclickdelegate = function. createdelegate (OO, oo. onclick );

$ Addhandler ($ get ("BTN"), "click", onclickdelegate );

</SCRIPT>

// Output: dongde

 

<Asp: scriptmanager id = "scriptmanager1" runat = "server"/>
<Input type = "button" value = "Click me" id = "BTN"/>

<Script language = "JavaScript" type = "text/JavaScript">

VaR oo = {

Textcontext: "dongde ",
Onclick: function (E, ARG)
{
Alert ("Hello:" + this. textcontext + "" + Arg );
}

}

// $ Addhandler ($ get ("BTN"), "click", oo. onclick); // events that add DOM elements across browsers

// Var onclickdelegate = function. createdelegate (OO, oo. onclick );

VaR onclickdelegate = function. createcallback (
Function. createdelegate (OO, oo. onclick), "Wish you success ");

$ Addhandler ($ get ("BTN"), "click", onclickdelegate );

</SCRIPT>

// Output: dongde wish you success


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.