Whether the function parameter is a value pass or a reference pass in Javascript

Source: Internet
Author: User

Objective

This problem actually puzzled me for a long time, but in the actual use always muddle through, do not want to go deep. Because of this attitude, in the process of learning Javascript, the level has been out of half a bucket of water state, many concepts and principles indefinitely, vague.

So, wrote a series of "JavaScript ..." is to correct the attitude, seriously study the characteristics and principles of JavaScript, to lay a solid foundation.

Today, this article explores the question of function-passing: is the function argument a reference to a value or a pass?

1. If a reference is passed

var name = ' JS '; function ChangeName (name) {    = ' Javascript ';  
console.log (' name changed: ' +name) // ' JS '

Why doesn't the name change?

If the reference is passed, the name should be changed. So it should be a value pass?

It may be normal for you to subconsciously feel the same, but it's because your experience tells you. Let's take a look at the next possibility by pressing the table.

2. If the value is passed

var obj = {name: ' JS '}; function changeobjname (obj) {    = ' Javascript ';    Console.log (' obj.name changed: ' +obj.name);   // obj.name Changed:javascript // {name: ' Javascript '}

Why is the parameter passed this time changed? Is it not a value pass? So that's a quote?

What about the above example? Start confused ...

3. Is the value pass or the reference pass??

In fact, in the world of Javascript, function transfer is not necessarily a way of transmitting parameters, mainly depends on the parameter passed. There are two main types of situations:

1. If the value type

Variables of basic data types are basically value types, such as String, numeric, Boolean, and so on.

The argument to a value type is a value pass, and modifications to this type parameter within a function do not affect the value that was originally passed in.

2. If the reference type

Composite data types such as objects, arrays, and so on are reference types.

Reference passes are reference passing, and modifications to this type parameter within a function affect the value that was originally passed in.

4. Mechanisms for in-depth function transfer

Whether the function parameter is a value pass or a reference pass in Javascript

Related Article

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.