美文网首页
js中String()、new String()探究

js中String()、new String()探究

作者: jadefan | 来源:发表于2019-09-29 08:51 被阅读0次

js中的基础类型有6种
5种基础类型:undefined null String Number Boolean
1中引用类型:Object

基础类型的string有如下定义方式:

  var str1 = "abc";
  var str2 = String('abc');
  var str3 = new String('abc');

这三种方式有什么区别吗?

  console.log(str1) //abc
  console.log(str2) //abc
  console.log(str3) //String {"abc"}

str1、str2是普通的字符串,str3是个对象?什么原因呢?

当 String() 和运算符 new 一起作为构造函数使用时,它返回一个新创建的 String 对象;
当不用 new 运算符调用 String() 时,返回原始的字符串

用 typeof 验证一下

  console.log(typeof str1) //string
  console.log(typeof str2) //string
  console.log(typeof str3) //object

用 instanceof 验证一下

  console.log(str1 instanceof String) //false
  console.log(str2 instanceof String) //false
  console.log(str3 instanceof String) //true
  
  console.log(str1 instanceof Object) //false
  console.log(str2 instanceof Object) //false
  console.log(str3 instanceof Object) //true

可以看出 str3 确确实实是个String对象了
再来看个有意思的事,如果给String加上自定义方法和属性呢?

  String.prototype.next = 'def'
  String.prototype.toNext=function(){ 
    return "def";
  }

  console.log(str1.next) //def
  console.log(str2.next) //def
  console.log(str3.next) //def

  console.log(str1.toNext()) //def
  console.log(str2.toNext()) //def
  console.log(str3.toNext()) //def

虽然 str1、str2不是对象,但可以用String上的方法和属性
再看,如果直接给str1、str2定义方法和属性呢?

  str1.end = 'xyz';
  str2.end = 'xyz';
  str3.end = 'xyz';

  console.log(str1.end) //undefined
  console.log(str2.end) //undefined
  console.log(str3.end) //xyz
  var xyzFn = function(){
    return 'xyz'
  };
  str1.toEnd = xyzFn; 
  str2.toEnd = xyzFn;
  str3.toEnd = xyzFn;
 
  console.log(str1.toEnd()) //Uncaught TypeError: str1.toEnd is not a function
  console.log(str2.toEnd()) //Uncaught TypeError: str1.toEnd is not a function
  console.log(str3.toEnd()) //xyz

可以看出str1、str2是不能定义自己的方法和属性的,而str3可以
这就能看出 js 的神奇部分了,什么原理呢?

来看包装对象和原始资料类型

数字、字符串、布尔三者,在JS中称为原始的(primitives)资料类型,而 new String(), new Number() 就是包装对象。

上面看到的str1、str2即为原始资料类型,str3为包装对象,通过typeof可以看出
但str1、str2不能自己定义属性和方法,却可以用String的属性和方法

原始资料类型的方法与属性是向包装对象"借来"的用的,自己本身并没有属性和方法

总结一下
1、第一种和第二种方法定义出来的是原始资料类型,并储存于栈中,并向包装对象(new ..())借来方法和属性.
2、第三种是 包装对象,栈中储存堆指针,堆中储存内容。

关于堆、栈、指针的问题,咱们继续学习

相关文章

网友评论

      本文标题:js中String()、new String()探究

      本文链接:https://www.haomeiwen.com/subject/avgxpctx.html