Type and function parameter Transfer type problem in JS

Source: Internet
Author: User

JS in the type:

2 large types: primitive types and objects.

The original type has a Boolean, number, string of three ordinary primitive types, plus null, undefined these two special primitive types

Objects are more, ordinary objects, built-in objects, global objects, functions, arrays, and so on.

function Parameter pass type:

For the original type, the value is passed, i.e. a copy of the incoming function is copied, and the modification within the function does not affect the external variable itself.

For an object type, an address is passed, and an external variable changes when the object is modified inside the function.

pay attention to this situation! refer to the following code:

var omyobj = {name: "Robert"}; function Test (obj) {   = "Ah!!! ";    = {Name: "Ah ~ ~ ~"};} Test (omyobj); alert (omyobj.name);

On the surface of the internal redefinition of the variable, it should be the result is "Ah ~ ~ ~" is right, but the result is "Ah!!! ”。

The reason is passed by address, and when test executes, it re-declares an obj local variable, which points to the external Omyobj object. So the first line of the function works. However, when obj = {name: ' ~ ~ ~ ' occurs, it changes the point of obj to the new object and does not affect the point of the external omyobj.

Type and function parameter Transfer type problem in JS

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.