js类型为string|string

JavaScript08

js类型为string|string,第1张

1、js分为基本数据类型和引用数据类型

2、string定义为基本数据类型,但是既然为基本数据类型为什么还可以调用方法呢?

let a = "hello world"

a.name = "张三"

console.log(typeof a)

console.log(a)

3、string还有另外一种创建的方法 new String("hello world")

let b = new String("hello world")

b.name = "赵四"

console.log(typeof b)

console.log(b.substring(0, 2))

new出来的字符串则是对象类型,在我们打印的时候可以看到,调用方法的时候是通过原型进行调用方法的

4、在使用字符串方法的时候内部是就行new了的操作从而调用的方法。

方法一:使用"+"将两个字符串"相加"

示例:

var longString = "One piece " + "plus one more piece."

//结果:longString值为:"One piece plus one more piece."

方法二: 要将多个字符串累积为一个字符串,还可以使用"+="操作符:

示例:

var result = ""

result += "My name is Anders"

result += " and my age is 25"

//结果:result值为"My name is Anders and my age is 25"

方法三:方法concat(),它完成与"+"相同的功能:string.concat(value1, value2, ...)

示例:

var str4="字符串连接"

str4=str4.concat("use concat function connec string")

//结果:"字符串链接use concat function connec string"