JS constructor
来源:互联网 发布:mac linux 双系统 编辑:程序博客网 时间:2024/05/28 17:05
Javascript确实是很烦人, 但也很诱人
这次记录constructor, 与之相关的, 还有typeof, ==, ===
试测测以下代码的结果
function demo(){
var str1="abc";
var str2=new String("abc");
var str3=new Array("abc");
alert(typeof str1);
alert(typeof str2);
alert(typeof str3);
alert(str1.constructor);
alert(str2.constructor);
alert(str3.constructor);
alert(str1 instanceof String);
alert(str2 instanceof String);
alert(str3 instanceof Array);
alert(str1==str2);
alert(str1==str3);
alert(str2==str3);
alert(str1===str2);
alert(str1===str3);
alert(str2===str3);
}
结果为
string
object
object
function String(){...}
function String(){...}
function Array(){...}
false //*1, string比较麻烦
true
true
true
true
false //*2, 2个实例进行==运算, 肯定false, 哪怕重写prototype valueOf()和toString()
false
false
false
*1, string在JS里很麻烦, 介于基本类型和对象之间, typeof "abc" -> string
"abc".constructor也是function String(){...}
但, "abc" instanceof String 却又是false, 在没有进一步了解之前, 权当是历史遗留问题吧
*2, ==运行, 对象跟string(非String)时, 会先调用对象的valueOf取值, 如果没有valueOf, 则调用toString, 然后跟string对比, 但Date对象是先toString, 然后才是valueOf
实例验证
结果:
true
true
false
看见上面的结果了吧, 有够烦人的
下面还有, 但好理解一点, 关于继承的
结果是
1:function A{this.name=name;}
2:true
3:false //构造器指针指向A
4:true
5:true
- JS constructor
- JS constructor
- JS constructor
- js constructor
- js constructor属性
- js constructor和prototype
- JS constructor属性
- js constructor问题!
- js constructor 和 instanceof
- js中的prototype与constructor
- js中的prototype和constructor
- JS Prototype constructor 转载之一
- js中的constructor 和prototype
- js 中的this,constructor ,prototype
- js中的constructor和prototype
- js 类与constructor属性
- js中的constructor与prototype
- js中typeof和constructor
- 【deep learning学习笔记】注释yusugomori的RBM代码 --- cpp文件 -- 模型测试
- 反编译Apk得到Java源代码
- 排序算法
- iOS: NSCoding Protocol的方法initWithCoder:
- 求根方函数程序
- JS constructor
- 【服务器+手机端项目】Android+Servlet+JSON综合案例之Android结合serlvet【三】
- 1350 求二叉树的深度
- extern "C"的作用
- H264 视频通信技术专题---丢包恢复技术之重传
- GNU Makefile学习专题(一) | 第一个实例
- hdu3593泛化背包
- (测试)Openbravo如何编写一个测试用例
- ora-01034 ora-27101 错误