Object knowledge points in private custom JavaScript

Source: Internet
Author: User
Tags date log tostring variable scope wrapper

Nonsense not much to say, first on the joke, then again,. Understanding this shows that your moral integrity is no longer.

Go to the barber's for a haircut after dinner ... Cut it out ... The boss asked me how to cut, I leisurely to a handsome cut ... Orgasms are often unthinkable .... Next to a hot hair in the oven of the aunt said don't be so embarrassed boss, people earn some money is not easy ...

First of all, if you are a master then please go out to the right, if you are rookie so congratulations, go on bar

Global objects

This kind of thing that can be used anywhere in a JavaScript program is a property of the global object. Then the object of the attribute is the global object.

When the JavaScript interpreter starts (or any web browser loads a new page), it creates a new global

object, and gives it a set of defined initial properties:

1. Global properties, such as Undefined,nan

2. Global functions, such as isNaN (), parseint ()

3. Constructors, such as date (), String ()

4. Global objects, such as math and JSON ()

A global variable is declared in the code, which is a property of the global object

The above code is purely space, on the code

var s= "Hello world!";   
var tempstr=s.substring (S.indexof ("") +1);

Have you ever wondered why s would call the substring () method?

The answer is that JavaScript converts a string value to an object by using the new string (s) as long as the attribute of the string s is referenced.

This object inherits the method of the string and is used to handle the reference to the attribute. Once the property reference is finished, the newly created object is destroyed.

For example, evidence:

var s= "test";   
s.len=4; Set a property to it   
var Temp=s.len;  
Console.log (temp)//undefined

Explanation: The second line of code creates a temporary string object and assigns the Len property a value of 4, but destroys the object immediately. The third line attempts to access the temporary properties

The modification only occurs on the temporary object, but the temporary object is not saved.

This leads to the idea that a temporary object created when a concept accesses a property of a string, number, or Boolean is called a wrapper object.

Knowing this can distinguish between the concepts of string and string objects, and so on. I don't really know what the big use is.

Wrapping Objects

First, what is the wrapper object, that is, the normal type of non-object is wrapped in some way into an object

The answer is that you can create a wrapper object with some built-in constructor string ()//number ()

var s=new String ("Marry You");  
Console.log (S); String {0: "M", 1: "A", 2: "R", 3: "R", 4: "Y", 5: "", 6: "Y", 7: "O", 8: "U", length:9}

You can use = = and = = To distinguish whether the packaging object, = = The original value and packaging object as equal, when "= = =" As unequal, can also pass typeof

The fundamental difference between the original value and the object is that the original value cannot be changed. Although the string appears to be somewhat exceptional.

Like what

var s= "Marry Me";  
Console.log (S.touppercase ()); Returns "MARRY"  
console.log (s);//"MARRY me" but does not change the value of S  
s=s.touppercase (); Console.log (s);//marry me

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

The original value cannot be changed, and no method can change an original value, for example, by specifying an index to modify characters in the string, JavaScript is prohibited. Actually the return to S is a new string, the previous s actually exists in memory, but it is not referenced (a little bit of doubt corresponds to this conclusion, thanks to the reminder of the @ Taishan Grand vision).

Object converted to original value

So how do you convert the object to the original value?

Common objects to strings and numbers to strings and so on

All objects inherit two conversion methods.

The first one is ToString ()

The function is to return a string that reflects the object.

The second one is valueof ()

The effect is that if any of the original values exist, it converts the object to its original value by default. Objects are compound values, and most objects cannot really represent

An original value that returns the object itself, not the original value, by default. (In fact, this knowledge can be dug deeper, I can only be tasted and stop)

For example: [1,2,4].tostring ();//"1,2,3"

(function (x) {Console.log ("Marry You");}). ToString (); "Function (x) {Console.log (" Marry You ");}"  
New Date (2022,2,2). ToString ()//"Sat 2222 00:00:00 gmt+0800 (China Standard Time)"  
      
var d=new Date (2020,2,22);  
D.valueof ();//1584806400000

So why does it have to be different, because many of the built-in classes have been modified for ToString ().

So if you want to use ToString () to react to the string type of this object

Object.prototype.toString (). Call (obj);//This gets the meaning of the original toString ()

such as: Object.prototype.toString.call ([1,2,4]); "[Object Array]"

If you use [1,2,4].tostring ()//"1,2,4" If you get this ghost thing, I can only comfort you, Buddy don't cry, stand up and masturbate

Variable scope

In this brief talk about the scope of variables

First, the scope of the variable is scoped to the variable, especially to the programmer who goes from the backend programmer to the front end that the scope here is not using the {} block level

Distinguishing scopes, but using a function function to differentiate between scopes is also called a block-level scope.

For example, for (Var i=0;i<1;i++) {Console.log (i);} console.log ("or in this scope I:", i)//0, or in the scope of the I:1

First in the function body, local variables have precedence over global variables of the same name.

var a= "global";  
function Testscope () {  
var a= "local";  
return A;  
}  
Testscope ()//"local"

So scope there are a lot to be sorted out, not to be continued.

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.