Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
haizhilin2013 opened this issue Jun 5, 2019 · 7 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the js JavaScript label Jun 5, 2019
@wenyejie
Copy link

wenyejie commented Jun 6, 2019

Array.prototype.join
`${}`
+=
= '' + ''

@myprelude
Copy link

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

@jiamianmao
Copy link

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

@censek
Copy link

censek commented Nov 20, 2019

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)

@MrZ2019
Copy link

MrZ2019 commented Nov 16, 2020

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

@ttop5
Copy link

ttop5 commented Nov 18, 2020

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

- a + b

- `${a}${b}`

- a.concat(b)

- 循环遍历组装

@xiaoqiangz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

8 participants