JavaScript Advanced Programming (second Edition) Learning (2)

Source: Internet
Author: User

Numeric conversions: There are 3 functions that can convert a non-numeric value to a value, number (), parseint (), parsefloat ().

For number () if it is

    • Boolean, True 1,false is 0;
    • Null is 0;
    • Undefined is Nan;
    • The string situation is much more: if there are numbers (whether integer or floating-point or valid hexadecimal number such as 0xf) will be converted to a number, such as "11A1" only output One, "0xa11" will be in accordance with 16 binary output. If the string is empty, it is 0; a character other than the above case is Nan;
    • In the case of an object, the object's valueof () method is called first, followed by the preceding rule, and if it is Nan, the ToString () is called, and the previous rule is converted again;

parseint () uses more, ignores whitespace in front of the string until the first non-whitespace character is found, and returns Nan if it is not a numeric character or minus sign. So "22.2" "22blue" both outputs are 22, the former because "." Not met, the latter because Blue does not meet. There is a divergence in the conversion of the binary, so you can specify the second parameter to specify his cardinality.

For example parseint ("Ten", "Ten"), parseint ("10", 8). When 16 is specified, the string can be without the preceding 0x.

Parsefloat () is similar to the above, except that it can parse a floating-point number, but a second "." appears. , stop the conversion. In addition, he can only do decimal, there is no second parameter, so will ignore the hexadecimal "0x" in front of the 0;

The ToString () method generally does not pass arguments, and in some cases it can pass a parameter as the cardinality of the output value.

var num=10; alert (num.tostring (// output 1010

To turn a value into a string, you can use the "+" operator to add it to a string ("") and the result of the output is a string

In ECMAScript, the object type is the basis for all instances of it. Object has the following properties and methods: (ECMA-262)

    • Constructor: Saves the function used to create the current object.
    • hasOwnProperty (PropertyName): Used to check whether a given property exists in an instance of the current object, where PropertyName must be specified as a string;
    • isPrototypeOf (object): Used to check if an incoming object is a prototype of another object.
    • propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated with for-in;
    • ToString (): Returns the string representation of the object;
    • ValueOf (): Returns the string, numeric value, or Boolean representation of the object.

Any data type can be used in a switch statement, or even an object. Second, each case value is not necessarily a constant, it can even be a variable, even an expression.

Switch ("Hello World") {    case ' hello ' + ' world ':          alert ("greeting was found");             Break ;     case "Goodbye":          alert ("Closing is found");            Break ;     default :          alert ("unexpected message was found");}

Parameters

The arguments inside the ecmascirpt are represented by an array, which is always the array that the function receives, not the parameters contained in the array. In fact, in the body of a function, this parameter array can be accessed through the arguments object, thus obtaining each parameter passed to the function. The arguments object is just like an array, and you can use an index to access its parameters.

// The following function does not define parameters, but this method is still possible. function Sayhi () {    alert ("Hello" +arguments[0]+arguments[1]);} Sayhi (// execution result Hello Mike GoodMorning

In the strict mode of JavaScript, the arguments object is restricted, as the following assignment method becomes invalid

function Doadd (NUM1, num2) {    arguments[1]=10;    Alert (arguments[// here Num2 has been assigned a value of 10 }

Each execution of the Doadd () function overrides the second parameter, changing the value of the second parameter to 10. Because the values in the arguments object are automatically reflected in the corresponding named parameters, modifying Arguments[1] also modifies the num2. However, reading these two values is not access to the same memory space: their memory space is independent, but the values are synchronized, the effect is unidirectional, and modifying num2 does not change the value of arguments[1]. In addition, if only one parameter is passed in, the value set by arguments[1] is not reflected in the named parameter (which is the argument in parentheses). Because the length of the arguments object is determined by the number of arguments passed in, rather than by the number of named arguments that define the function. In addition, no pass-worthy named parameters are automatically assigned to undefined.

ECMAScript variables may contain two different data types: basic data types (simple data segments) and reference type values (objects consisting of multiple values)

The basic data type (undefined,null,boolean,number,string) in 5 is accessed by value because the actual value saved in the variable can be manipulated. The value of a reference type is an object that is stored in memory, and unlike other languages, JavaScript does not allow direct access to the in-memory location and cannot directly manipulate the object's memory space. When you manipulate an object, you actually manipulate the object's reference, not the actual object. Thus, the value of the reference type is accessed by reference.

Copy the value of a variable: there are differences when copying base type values and reference types from one variable to another.

/* in the following example, the NUM1 is saved in 5, num1 to initialize num2, num2 also saved 5, but the two 5 is completely independent, not affected by each other */ var num1=5; var num2=num1; /* the value of the reference type is different, and the variable obj1 in the following example holds a new instance of the object. Then, given to the OBJ2, the even one points to the same object. When Obj1 adds name, OBJ2 also has this attribute */var obj1=New  Object (); var obj2=Obj1;obj1.name= "Mike"// output Mike;

Passing parameters

The parameters of all functions in ECMAScript are passed by value.

/*parameter passing of the base data type is passed by value, and modifications within the function cannot be passed to the external*/functionAddten (num) {num+ = 10; returnnum;}varCount=20;varresult=Addten (count); alert (count);//20 No changealert (result);// -/*parameter passing of a value of a reference type is still a value pass, and if it is a reference pass, then the second modification should be "Jim".
But when you access the name attribute, you still get Mike. Description even if the value of the parameter is modified inside the function, the original reference is still
No change. In fact, when the function internally rewrites obj, it gets a local object that is reclaimed by the garbage collection mechanism after execution. */functionsetName (obj) {obj.name= "Mike"; Obj=NewObject (); Obj.name= "Jim"; Alert ("Local:" +obj.name);//local object when Jim}functionsetName (obj) {obj.name= "Mike"; //obj={};Obj.name= "Jim";//The result is Jim, which overrides the previous assignment.}varperson=NewObject (); SetName (person); alert (person.name) ;//Mike

JavaScript Advanced Programming (second Edition) Learning (2)

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.