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
实例验证
function demo(){
var A=function(n){
this.name=n;
}
A.prototype.valueOf=function(){return this.name;}
A.prototype.toString=function(){return "A: "+this.name;}
var B=function(n){
this.name=n;
}
B.prototype.valueOf=function(){return this.name;}
B.prototype.toString=function(){return "B: "+this.name;}
var c="david";
var a=new A("david");
var b=new B(c);
alert(c==a);
alert(c==b);
alert(a==b);
}
结果:
true
true
false
看见上面的结果了吧, 有够烦人的
下面还有, 但好理解一点, 关于继承的
function demo(){
var A=function(name){
this.name=name;
}
A.prototype.showName=function(){alert(this.name);} //the function return this, "this" is the pointer of instance
A.prototype.showSex=function(){alert("male");}
A.prototype.city="shenzhen";
var B=function(name, age){
A.call(this, name);
this.age=age;
}
B.prototype=A.prototype;
B.prototype.showAge=function(){alert(this.age);}
var b=new B("david", 31);
try {
alert(b.constructor); //*1
alert(b.constructor==A); //*2
alert(b.constructor==B); //*3
alert(b instanceof A); //*4
alert(b instanceof B); //*5
} catch (exception) {
alert(exception);
}
}
结果是
1:function A{this.name=name;}
2:true
3:false //构造器指针指向A
4:true
5:true
分享到:
相关推荐
JavaScript的constructor属性[文].pdf
js-constructor:JavaScript
JS:typeof instanceof constructor prototype区别
本文实例讲述了js核心基础之构造函数constructor用法。分享给大家供大家参考,具体如下: 在js中,可以利用构造函数来创建特定类型的对象,其中,有一些原生的构造函数,Object、Array、等等,所以,当type of ...
JavaScript Boolean.constructor()方法返回一个引用到创建实例的原型布尔函数。 语法 boolean.constructor() 返回值: NA 下面是参数的详细信息: 返回创建此对象的实例。 例子: <html> <head> <...
例如,在Chrome下调试如下程序,很清楚的展示了这点: 然而事情并不是这么简单。再看下面的代码: 很显然,这个时候obj的constructor已经不再是创建它的函数,注意... 我觉得Javascript的设计本意是让每个对象的con
Watchface-Constructor Please check [our article] (https://yalantis.com/blog/building-a-customizable-constructor-for-designers-of-android-wear-watch-face-apps) This is simple watchface constructor ...
至少每个尝试JavaScriptOO的程序员都花费很多精力用在面向对象机制的模拟上而非业务本身. 这对Java,C++甚至Php的开发者来讲都是难以想象的. 更糟糕的是模拟OO对于JavaScript高级程序员都有着邪恶的吸引. 因为干这个...
Constructor:js构造函数
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数,除了创建对象,构造函数(constructor) 还做了另一件有用的事情—自动为创建的新对象设置了原型对象(prototype object)
代码如下: [removed] Function.prototype.... }[removed] 说下上面代码里面 T.constructor = this这句话,我感觉这句话没有什么实际作用, 本身T.constructor应该是为Funtion,为什么要给它设定为Funtion的实例呢,
实例 c# constructor
分析javascript中 prototype __proto__ constructor之间的关系
BOA Constructor是Python 下用GTK进行图形编程的软件,界面很象Delphi. Getting Started Guide for boa Constructor boa constructor 教程
本文档时关于Java的反射的一个组件Constructor的描述和讲解
Boa-constructor 是一个很好的wxpython gui设计软件
constructor 属性返回对创建此对象的数组函数的引用。本文给大家介绍JavaScript中的constructor ,需要的朋友参考下吧
javascript new后的constructor属性.docx
深化浅析JavaScript中的constructor_.docx