Giter Club home page Giter Club logo

Comments (1)

wingmeng avatar wingmeng commented on July 24, 2024

这道题涉及 javascript 的数据类型,主要分两类:

  1. 基本数据类型: Number、String、Boolean、Null、 Undefined、Symbol(ES6)
  2. 引用数据类型: Object(除基本数据类型外的都是对象,Array、Function、RegExp等)

基本数据类型是直接按值存放在内存中的简单数据段,可以直接保存变量中的实际值,所以第 1 题控制台输出 2,a 是基本数据类型。

引用数据类型是存放在内存中的对象,其变量其实是一个指针,保存了指向内存对象的引用地址,所以第 2 题的 arr 变量其实只是将引用地址复制了一份给了 arr_copy 变量,实际上它们共同指向同一个 [1,2,3,4] 内存对象,所以 arr_copy push 了一项,是对 [1,2,3,4] 的修改,因此 arr 变量的值也跟着变为了 [1,2,3,4,5],第 4 题也是同样的道理。

第 3 题和第 5 题有所不同,虽然是引用数据类型,但代码中对 copy 的变量进行了重新赋值,相当于在内存中创建并指向了一个新的内存对象,因此对 copy 变量的修改不会影响到原先变量的值。

总结一下答案:

  1. 输出:2
  2. 输出:[1,2,3,4,5]
  3. 输出:[1,2,3,4][1,2,3,4,5]
  4. 输出:过客
  5. 输出:sunshine过客

from fe-practice-hard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.