A JS question about reference passing and value passing

Source: Internet
Author: User

First look at question 1

var obj = {name: ' A '}function  Bar (obj) {    console.log (obj.name);     = {Name: "B"};    Console.log (obj.name)}console.log (bar (obj)) console.log (obj.name)

In the chrome console, do the following to find out what to print

a b a

Question 2, a little bit of a change

var obj = {name: ' A '}function  Bar (obj) {    console.log (obj.name);     = "B";    Console.log (obj.name)}console.log (bar (obj)) console.log (obj.name)

In the chrome console, do the following to find out what to print

Abb

Question 3, change it a little bit.

var obj = {name: ' A '}function  Bar (obj) {    console.log (obj.name);     var obj = {name: "B"};    Console.log (obj.name)}console.log (bar (obj)) console.log (obj.name)

In the chrome console, do the following to find out what to print

Aba

Question 4, continue to change

var obj = "name"function  Bar (obj) {    console.log (obj);     var obj = "age";    Console.log (obj)}console.log ("Bar (obj)") console.log (obj )

Find this to print

Nameagename

Question 5, continue to change

var obj = "name"function  Bar () {    console.log (obj);     var obj = "age";    Console.log (obj)}console.log ("Bar (obj)") console.log (obj )

Discover Print in sequence

Undefinedagename

Question 6, or continue to change

var obj = "name"function  Bar () {    console.log (obj);      = "Age";    Console.log (obj)}console.log ("Bar (obj)") console.log (obj )

Look what you're going to print this time.

Nameageage

If you're right, you understand reference passing and value passing, understanding local variables and global variables, and if you're wrong, think about it.

A JS question about reference passing and value passing

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.