Prototype Object Objects Learning _prototype

Source: Internet
Author: User
Tags eval extend hash json object object shallow copy static class tojson
The Object is used by Prototype as a namespace; The It just keeps a few new methods together, which are intended for namespaced access (i.e. starting with "Object." ).
The above namespace personal understanding is equivalent to the static class in C #, providing the meaning of the tool function, and the namespace in C # should not be a concept. Because the namespaces in C # are not followed directly by a method, it is certain that an object then invokes the method, but it is somewhat similar to the namespaces in C + +
Clone
Extend
Inspect
IsArray
Iselement
Isfunction
Ishash
Isnumber
Isstring
isundefined
Keys
ToHTML
Tojson
Toquerystring
Values
Copy Code code as follows:

Creating object objects in the form of anonymous function calls
(function () {

Gets the string representation of the type, (Prototype Learning--Tool function learning ($ method)) This log contains detailed instructions
function GetClass (object) {
Return Object.prototype.toString.call (Object)
. Match (/^\[object\s (. *) \]$/) [1];
}

The inheritance method, a very simple transcription mechanism, is to copy all the properties and methods in the source to destination, and if it is a reference type, source and destination will point to the same address
function extend (destination, source) {
For (Var property in Source)
Destination[property] = Source[property];
return destination;
}

Returns the string representation of object
function inspect (object) {
try {
if (isundefined (object)) return ' undefined ';
if (object = = null) return ' null ';
Return object.inspect? Object.inspect (): String (object);
catch (e) {
if (e instanceof rangeerror) return ' ... ';
Throw e;
}
}

Returns the JSON (JavaScript object notation) object
function Tojson (object) {
var type = typeof object;
Switch (type) {
Case ' undefined ':
Case ' function ':
Case ' unknown ': return;
Case ' Boolean ': Return Object.ToString ();
}

if (object = = null) return ' null ';
if (Object.tojson) return Object.tojson ();
if (Iselement (object)) return;

var results = [];
For (var property in object) {
var value = Tojson (Object[property]);
if (!isundefined (value))
Results.push (Property.tojson () + ': ' + value);
}

Return ' {' + results.join (', ') + '} ';
}

Returns the query string, for example: param1=value1¶m2=value2
function Toquerystring (object) {
return $H (object). toquerystring ();
}

Returns an HTML string
function ToHTML (object) {
Return object && object.tohtml? Object.tohtml (): String.interpret (object);
}

Gets all keys of object
function keys (object) {
var results = [];
For (var property in object)
Results.push (property);
return results;
}

Gets all the value of object
function values (object) {
var results = [];
For (var property in object)
Results.push (Object[property]);
return results;
}

Copies all properties and methods in object to an empty object and returns
function Clone (object) {
Return extend ({}, object);
}

To determine if object is a basic Dom Element
function Iselement (object) {
Return!! (Object && object.nodetype = 1);
}

Determines whether object is an array
function IsArray (object) {
return GetClass (object) = = = "Array";
}

A Hash object that determines whether object is prototype
function Ishash (object) {
return object instanceof Hash;
}

Determine if object is a function
function Isfunction (object) {
Return typeof object = = "function";
}

Determines whether object is a string
function Isstring (object) {
return GetClass (object) = = = "String";
}

Determine if object is a number
function Isnumber (object) {
return GetClass (object) = = = "Number";
}

Determine if object is already defined
function isundefined (object) {
Return typeof object = = "undefined";
}

Return Object Object
Extend (Object, {
Extend:extend,
Inspect:inspect,
Tojson:tojson,
Toquerystring:toquerystring,
Tohtml:tohtml,
Keys:keys,
Values:values,
Clone:clone,
Iselement:iselement,
Isarray:isarray,
Ishash:ishash,
Isfunction:isfunction,
Isstring:isstring,
Isnumber:isnumber,
isundefined:isundefined
});
})();

Inspect method:
Copy Code code as follows:

Object.inspect ()//-> ' undefined '
Object.inspect (NULL)//-> ' null '
Object.inspect (FALSE)//-> ' false '
Object.inspect ([1, 2, 3])//-> ' [1, 2, 3] '
Object.inspect (' hello ')//-> "' Hello '"

Tojson Method:
Note that there is a recursive invocation of the process var value = Tojson (Object[property]), and finally returns a string representation in JSON format, and here's a look at the example:
Copy Code code as follows:

var data = {name: ' Violet ', Occupation: ' character ', age:25, Pets: [' frog ', ' Rabbit ']};
/* ' {' name ': "Violet", "occupation": "Character", "Age": ", Pets": ["Frog", "Rabbit"]} ' * *
Remember to enclose the JSON string that is returned by eval, otherwise the error occurs, where parentheses act as a force for evaluation.
Otherwise, the JSON string will be treated as a compound statement, with the ("name":) as the label, and then parsing the subsequent content will be an error
var obj=eval (' (' +object.tojson (data) + ') ');
alert (obj.name);

Toquerystring Method:
Use object to create a hash object, then call the hash object's Toquerystring method, and return the call result, when talking about the hash object in detail toquerystring method.
This method is commonly used when calling Ajax.request, and here's a look at the example:
Copy Code code as follows:

Object.toquerystring ({action: ' Ship ', order_id:123, fees: [' F1 ', ' F2 '], ' label ': ' A demo '})
-> ' Action=ship&order_id=123&fees=f1&fees=f2&label=a%20demo '

tohtml Method:
If the object parameter passed in is undefined or null returns an empty string
Alert (object.tohtml ())
Alert (object.tohtml (NULL))
If object defines the Tohtml method, then the Tohtml method of the object is invoked, and no one calls the static method interpret of string, which is to determine whether the object is null or NULL to return '. Otherwise, the object's ToString method is invoked and the result of the call is returned
Copy Code code as follows:

Object.extend (String, {
Interpret:function (value) {
return value = = null? ': String (value);
},
Specialchar: {
' \b ': ' \\b ',
' t ': ' \\t ',
' \ n ': ' \\n ',
' \f ': ' \\f ',
' \ r ': ' \ R ',
'\\': '\\\\'
}
});

Here's a look at the example:
Copy Code code as follows:

var Bookmark = class.create ({
Initialize:function (name, URL) {
THIS.name = name;
This.url = URL;
},
Tohtml:function () {
Return ' <a href= ' #{url} ' href= ' #{url} ' >#{name}</a> '. interpolate (this);
}
});
var API = new Bookmark (' Prototype API ', ' Http://prototypejs.org/api ');
Object.tohtml (API);
-> ' <a href= ' http://prototypejs.org/api ' href= ' http://prototypejs.org/api ' >prototype api</a> '

keys and Values methods:
Take a look at the example and you will not say more:
Copy Code code as follows:

Object.keys ()
-> []
Object.keys ({name: ' Prototype ', version:1.5}). Sort ()
-> [' name ', ' version ']
Object.values ()
-> []
Object.values ({name: ' Prototype ', version:1.5}). Sort ()
-> [1.5, ' Prototype ']

Clone method:
' {} ' is the direct amount of an empty object, equivalent to the new object ()
Copy Code code as follows:

var o = {name: ' Prototype ', version:1.5, authors: [' Sam ', ' contributors ']};
var O2 = Object.clone (o);
O2.version = ' 1.5 weird ';
O2.authors.pop ();
O.version//-> 1.5
O2.version//-> ' 1.5 weird '
O.authors//-> [' Sam ']
ouch! Shallow copy!, Attention here!

Isxxx method not to say it.

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.