Skip to content

[js] 第51天 字符串相连有哪些方式?哪种最好?为什么? #200

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第51天 字符串相连有哪些方式?哪种最好?为什么?

Activity

wenyejie

wenyejie commented on Jun 6, 2019

@wenyejie
Array.prototype.join
`${}`
+=
= '' + ''
myprelude

myprelude commented on Jun 13, 2019

@myprelude
  • ES6
    ${var}
    简单,方便,但是不兼容低版本浏览器
  • ES5
    ""+"" ''+''
    兼容性好,但是比较麻烦考验心智,如果拼接的有""''时需要\转义
jiamianmao

jiamianmao commented on Aug 10, 2019

@jiamianmao

最好应该是 模板字符串``
虽性能不是最佳,但很多时候更需要考虑的可读性。
函数式编程就是追求的可读性而不是性能。

censek

censek commented on Nov 20, 2019

@censek
var a = "aaaa"
var b = "bbbbb"

// 方法一: “+”
var c = a + b
console.log("c:", c)

// 方法二: “join("")”
var d = []
d.push(a,b)
console.log("d:", d.join(""))

// 方法三:模版字符串 `${}`
var e = `${a}${b}`
console.log("e:", e)
smile-2008

smile-2008 commented on Nov 16, 2020

@smile-2008
  • ES6
    ${var}
    简单,方便,但是不兼容低版本浏览器
  • ES5
    ""+"" ''+''
    兼容性好,但是比较麻烦考验心智,如果拼接的有""''时需要\转义
ttop5

ttop5 commented on Nov 18, 2020

@ttop5
const a = 'aaaaa';
const b = 'bbbbb';

- a + b

- `${a}${b}`

- a.concat(b)

- 循环遍历组装
xiaoqiangz

xiaoqiangz commented on Jun 7, 2022

@xiaoqiangz

应该是模板字符串 ${ a } 然后 字符串 '+'

panpanxuebing

panpanxuebing commented on Dec 12, 2024

@panpanxuebing

运算符+,模板字符串,concat
简单的拼接用 + 就行,复杂的用模板字符串

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@wenyejie@haizhilin2013@ttop5@xiaoqiangz

        Issue actions

          [js] 第51天 字符串相连有哪些方式?哪种最好?为什么? · Issue #200 · haizlin/fe-interview