JSON data and objects

Source: Internet
Author: User
Tags object serialization sin square root

In JS, like numeric types, string types, and Boolean types, these can no longer be split and belong to the base type. In contrast to a complex type: An object type, which is itself composed of several other types.

There are two ways to create an object, one, a new object (), and two, a literal creation method, Var obj={}. The data in the object is always in the form of a key-value pair, such as: Name: "Monkey King", when there are multiple key-value pairs, at the end of each has "," to differentiate. At the same time, an object's properties can be either an object or a function.

What do we do when we write data to an object and we need to transfer it or store it on a computer? A computer must be in binary form when it stores, processes, and transmits data. So when we want to transfer or store an object, we need to first convert the object to a string and other data that can be transferred, and the process of converting an object to a string is called an object serialization.

There are two main ways to serialize objects, the first of which is called XML (extend Markup Language) data:

<object>
<name> </name>
<age>28</age>
<brother>
<name> Monkey King </name>
<age>99</age>
</brother>
</object>

The second way is the JSON (JavaScript Object Notation) data
{Name: "Age:28,brother:{name", "Monkey King", age:99}}

The format of the JSON data and the JavaScript literal creation object are basically the same format, the only difference is that there is no function in the JSON data, json.stringify function, you can convert an object to a JSON string, this function

Automatically removes the function from the object.

So when we serialize the object to facilitate the transmission of the data, but when we need to process the data, we have to parse the data, when the client requests from the server to the data, is the serialized object, so before processing the data, before the serialized string into the object, This process is called data parsing, and the JSON data is parsed using the Json.parse () function.

In particular, when our client's data needs to interact with the server, we receive the data from the server is through the JSON serialized data, after the client receives the data to use this data, we need to parse these JSON data, It is not generally necessary to serialize the data in the first way.

Include: Boolean objects, String objects, numeric objects, mathematical objects, and so on

Boolean object: is a basic type, only true and false, can only be created by new and cannot be created by literal

If the parameter of the Boolean constructor is not a Boolean value, the argument is converted to a
Boolean value. If the parameter is 0,-0, NULL, FALSE, NaN, undefined,
or an empty string (""), the value of the generated Boolean object is False. Any other value,
including any object or string "False", a value of true is created. ToString () Converts the object to a string type.

String object: The creation of a string object is typically created by literal methods

/*--------------------------------*/
. indexof returns the index value of the first discovered given string from a string object,
Returns 1 if not found
var index = "ABCDEFGABCDEFG". IndexOf ("B");
Console.log (index);
. LastIndexOf finds the index value of the last discovered given string from a string.
/*--------------------------------*/
. match determines whether a string satisfies a regular expression
var Gex =/^1\d{10}$/;
if ("1361380g129". Match (Gex)) {
Console.log ("mobile phone number");
}else{
Console.log ("Not mobile phone number");
}
/*--------------------------------*/
. Slice string interception,
var str8 = "0123456789";
Console.log (Str8.slice (2,str8.length));
/*--------------------------------*/
. Split splits a string by a character, and the return value is an array
var STR9 = "Iphone,samsung,nokia";
var phones = Str9.split (",");
Console.log (phones);
/*--------------------------------*/
. substr intercepts the string, starting at the first position and how many characters to intercept
Console.log (STR8.SUBSTR (3,5));
/*--------------------------------*/
. substring from an index location (including) to an index location (not included)
Console.log (str8.substring (3,5));
/*--------------------------------*/
. toUpperCase to capitalize all English letters in a string
.... toLowerCase ..... Convert to lowercase
Console.log ("ABc". toUpperCase ());
Console.log ("AbC". toLowerCase ());
/*--------------------------------*/
. Anchor to enclose a string in a tag
var str10 = "Sunhuayu";
Console.log (Str10.anchor ("a"));
/*--------------------------------*/
. Replace replaces some characters in a string with new characters.
The third argument, G for all substitutions, I for ignoring case
var str11 = "abc123abc123";
Console.log (Str11.replace ("A", "Z", "G"));

Mathematical objects:

var n1 =-3.5;
Math.Abs Take absolute value
Console.log (Math.Abs (N1));

Math.sin sine function, the parameter of the function must be a radian system
Angle to Radian Deg/180*math.pi
Console.log (Math.sin (MATH.PI/6));
Math.Cos ();

MATH.CBRT Cube Root
Math,sqrt Square Root
Console.log (MATH.CBRT (27));

Math.ceil rounding up
Math.floor Downward rounding
Console.log (Math.ceil (-1.2));

Math.pow power operation. Several times in the party
Console.log (Math.pow (9,0.5));

Math.random () generates a random number greater than or equal to 0 and less than 1
Math.floor (Math.random () *11) +10

JSON data and objects

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.