JavaScript对象是属性的集合, 属性表现为常说的键值对, 当属性的值是function的时候,称为对象的方法。
设对象o有方法m,调用此方法的方式为o.m()。
从以下代码可以看到, strings是有属性和方法的。
var s = "hello world";
var word = s.substring(s.indexOf(" ") + 1, s.length);
Strings不是对象类型, 为什么会拥有属性?
无论什么时候, 只要访问string的属性, JavsScript会使用new String(args)利用原始类型的string产生一个对象类型。
这个对象会继承string的方法, 用来访问对象的属性, 一旦属性访问结束, 刚刚创建的对象类型的string将被丢弃。
对Numbers和Booleans来说, 如果要访问他们的属性或者方法, 也使用了同样的机制。
同为原始类型的null与undefined并不适用此方法。 如果仍想访问他们的属性,你会得到一个TypeError。
var s = "test";
s.len = 4;
var t = s.len;
打印t得到的运行结果是undefined。 第二行的代码创建了临时String对象, 将4赋值给临时对象的属性len, 然后这个临时对象被丢弃。
第三行代码从原始的string s又创建了新的String对象, 然后将对象的len属性的值赋予t, 但是这个属性是不存在的, 所以结果得到了undefined。
从演示可以看出strings, numbers, boolean可以像对象一样让用户访问其属性或者方法。但是如果要对属性进行设置, 这样的行为就会被忽略。
因为变化发生在临时对象上,并不持久。
在访问string, number还有boolean的属性产生的临时对象称为wrapper objects, 很少有需要区分原始类型与他们的wrapper对象的时候。
通常情况下只需要知道他们和对象的区别, 还有他们的属性是只读的, 而且不能够定义新的属性。
wrapper对象是可以手动产生的。
var s = "test", n = 1, b = true;
var s_w = new String(s);
var n_w = new Number(n);
var b_w = new Boolean(b);
必要的时候JavaScript会将wrapper对象转换为原始类型, 所以以上的s_w, n_w, b_w "大部分" 情况下与s, n, b有同样的表现。
使用 == 比较原始类型与其wrapper对象会得到true。
使用 === 会得到false。
使用typeof也会得到不同的结果。
分享到:
相关推荐
Kafka_The Definitive Guide_Real-Time Data and Stream Processing at Scale, 2017年新书,值得一看,欢迎下载!
Kafka The Definitive Guide Real-Time Data and Stream Processing at Scale 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
The Definitive Guide to PC-BSD.pdf The Definitive Guide to PC-BSD.pdf
JavaScript The Definitive Guide 5th 权威指南 第五版 英文 教材 非常经典的javascript学习资料
The Definitive Guide to Django - Web Development Done Right(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
The Definitive Guide to Jython, written by the official Jython team leads, covers the latest Jython 2.5 (or 2.5.x) from the basics to the advanced features. This book begins with a brief introduction ...
The Definitive Guide to PC-BSD 2010
Hadoop The Definitive Guide 2nd Edition-指南第二版 这个是Hadoop指南英文第二版,高清文字版本,可以复制文字内容。
The Definitive Guide to the ARM® Cortex®-M0 and Cortex-M0+ Processors, Second Edition explains the architectures underneath ARM’s Cortex-M0 and Cortex-M0+ processors and their programming ...
[removed] The Definitive Guide, 4th Edition包括电子版书和代码
elastic search guide which is a framework based on lucene
Kafka_The Definitive Guide_Real-Time Data and Stream Processing at Scale
Kafka The Definitive Guide Real-Time Data and Stream Processing at Scale 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Learn everything you need to master the AdonisJs framework, including topics such as interacting with a database, rendering templates, writing asynchronous code, and hosting sites with SSL.
JavaScript The Definitive Guide 6th Edition.pdf
Kafka The Definitive Guide Real-time data and stream processing at scale 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn...
从sarifi离线保存打包而来,是2017年12月版本,应该很接近最后的发布版本了
JavaScript The Definitive Guide JavaScript The Definitive Guide
JavaScript The Definitive Guide