04 variables, scope, memory

Source: Internet
Author: User

Beginning

This chapter mainly deals with 3 questions

    1. Variable
    2. Type judgment
    3. Scope


Variable.

Base type & reference type.

The underlying type is inside the stack. Reference type station put an address inside, heap inside put actual content.

Feel like. NET value types and reference types.

The problem that comes out is.

Problem with reference type underlying type replication.

In theory, replication is on top of the stack .

Value types are on the stack . So each copy is new.

 var i = 0;var ii = i;ii = 1;console.log(i);console.log(ii);

The result is naturally 0 & 1.

Reference type He is just a reference on the buyers. The actual content is on heap memory.

So when you copy a reference type, you just copy a reference. In fact, you're pointing at the same address.

var o = { a: 1 };var o1 = o;o1.a = 2;console.log(o.a);console.log(o1.a);

Results 2,2.

That's it.

Sounds like a deep copy & a sneak copy .

is for reference types.

A sneak copy is just that simple copy. The reference address is still the same.

A deep copy is a copy of the data in the heap memory and becomes two completely independent individuals.



A bit deeper in the book, thefunction parameter problem

var o = {    a: ‘test‘}function a(obj){    obj.a = ‘test1‘;}a(o);console.log(o);

Parameter passing is actually equivalent to a sneak copy .

If the argument is a reference type, the change is made outside the call. is going to change.



Type judgment

Used to generally use typeOf and then get the type

console.log(typeof(‘sadasd‘))console.log(typeof({}))console.log(typeof(function(){}))console.log(typeof([]))console.log(typeof(1));console.log(typeof(NaN));console.log(typeof(Number.MIN_VALUE));console.log(typeof(Infinity));console.log(typeof("123"));console.log(typeof(true));console.log(typeof(window));console.log(typeof(document));console.log(typeof(null));console.log(typeof(eval));console.log(typeof(Date));console.log(typeof(sss));console.log(typeof(undefined));


String
Object
function
Object
Number
Number
Number
Number
String
Boolean
Object
Object
Object
function
function
Undefined
Undefined

Various. The main problem is still too much object

Array is an object.

Object is an object.

Null is an object.

There are two ways to solve this problem.

    1. With only his attributes.

such as Array. Push method

if(o.push)

So it can be judged.

You can also use the instanceof

if(o instanceof Array)if(o instanceof RegExp)

You can do it.


Scope

He said 2 concepts.

1 execution environment.

First, there is a Global environment window.

Others are local environments.

There is an environment buyers this concept. When entering a function, the environment of the function is push into the environment buyers.

Pop up after execution. and destroy the environment and variables for this function.

And window is always there, knowing that the web is off, the browser is off, and so on.

2 Scope Chain

This is relatively simple.

The execution of each function will have a chain of scopes.

It has its own variables, and the external environment.

The external environment is the same.

Has formed a chain of such a level.

Superiors cannot access subordinates. The lowest level of access to all. The relationship.


Here's an example.

var a = 1;function funca(){    var b = 2;    function funcb() {        var c = 3;    }    funcb();}funca();

The example is simple, but it can still explain the problem.

is to find the internal variables of your environment first and then look outside.



He mentioned it in the back. Extend scope and block-level scopes.

He says you can use try catch & with to extend the scope

function test1(){    var qs = "?debug=true"    with(location)    {        var url  = href +qs;    }    return url;}function test2() {    try {        var aaa = "aa";    } catch (e) {    }    return aaa;}

Both can access the variables declared inside the {}. The book says it is extended scope.

But I think this is because JavaScript doesn't have block-level scope.

Like what

if(xxx) {    var a = 0;}

This a can be accessed outside of {}. Because JavaScript is not block-scoped.

He and {} are a scope. So you can access it.

I think with & Try Catch is a reason.



04 variables, scope, memory

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.