JS constructor属性
来源:互联网 发布:windows多用户同时登录 编辑:程序博客网 时间:2024/05/28 19:23
http://liuguofeng.iteye.com/blog/1287810
constructor属性始终指向创建当前对象的构造函数。比如下面例子:
- var arr = [1, 56, 34, 12]; // 等价于 var foo = new Array(1, 56, 34, 12);
- document.writeln(arr.constructor === Array);// true
在看下面的例子
- var foo = new Function();
- var Foo = function() { };
- document.writeln(Foo.constructor === Function); // true
- // 由构造函数实例化一个obj对象
- var obj = new Foo();
- document.writeln(obj.constructor === Foo); // true
- // 将上面两段代码合起来,就得到下面的结论
- document.writeln(obj.constructor.constructor === Function); // true
- 但是当constructor遇到prototype时,有趣的事情就发生了。
- 我们知道每个函数都有一个默认的属性prototype,
- 而这个prototype的constructor默认指向这个函数。如下例所示:
- Java代码
- function Person(name) {
- this.name = name;
- };
- Person.prototype.getName = function() {
- return this.name;
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Person);// true
- document.writeln(Person.prototype.constructor === Person); // true
- // 将上两行代码合并就得到如下结果
- document.writeln(p.constructor.prototype.constructor === Person); // true
- function Person(name) {
- this.name = name;
- };
- Person.prototype.getName = function() {
- return this.name;
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Person);// true
- document.writeln(Person.prototype.constructor === Person); // true
- // 将上两行代码合并就得到如下结果
- document.writeln(p.constructor.prototype.constructor === Person); // true
- 当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),
- constructor属性的行为就有点奇怪了,如下示例:
- Java代码
- function Person(name) {
- this.name = name;
- };
- Person.prototype = {
- getName: function() {
- return this.name;
- }
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Person); // false
- document.writeln(Person.prototype.constructor === Person); // false
- document.writeln(p.constructor.prototype.constructor === Person); // false
- function Person(name) {
- this.name = name;
- };
- Person.prototype = {
- getName: function() {
- return this.name;
- }
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Person); // false
- document.writeln(Person.prototype.constructor === Person); // false
- document.writeln(p.constructor.prototype.constructor === Person); // false
- 为什么呢?
- 原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
- Java代码
- Person.prototype = new Object({
- getName: function() {
- return this.name;
- }
- });
- Person.prototype = new Object({
- getName: function() {
- return this.name;
- }
- });
- 而constructor属性始终指向创建自身的构造函数,
- 所以此时Person.prototype.constructor === Object,即是:
- Java代码
- function Person(name) {
- this.name = name;
- };
- Person.prototype = {
- getName: function() {
- return this.name;
- }
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Object); // true
- document.writeln(Person.prototype.constructor === Object); // true
- document.writeln(p.constructor.prototype.constructor === Object); // true
- function Person(name) {
- this.name = name;
- };
- Person.prototype = {
- getName: function() {
- return this.name;
- }
- };
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Object); // true
- document.writeln(Person.prototype.constructor === Object); // true
- document.writeln(p.constructor.prototype.constructor === Object); // true
- 怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
- Java代码
- function Person(name) {
- this.name = name;
- };
- Person.prototype = new Object({
- getName: function() {
- return this.name;
- }
- });
- Person.prototype.constructor = Person;
- // 如此也可以
- /*
- Person.prototype = new Object({
- constructor:Person,
- getName: function() {
- return this.name;
- }
- });
- Person.prototype = {
- constructor:Person,
- getName: function() {
- return this.name;
- }
- };
- */
- var p = new Person("ZhangSan");
- document.writeln(p.constructor === Person); // true
- document.writeln(Person.prototype.constructor === Person); // true
- document.writeln(p.constructor.prototype.constructor === Person); // true
- js constructor属性
- JS constructor属性
- js 类与constructor属性
- js Number对象 :属性constructor
- js深入学习-js prototype constructor属性区别
- JS constructor
- JS constructor
- JS constructor
- js constructor
- js中判断数据类型的属性typeof、instanceof、 constructor、 prototype
- 揭开js之constructor属性的神秘面纱
- js之constructor属性/instanceof运算符/原型的理解
- JavaScript constructor属性
- constructor属性详解
- javascript constructor 属性
- JavaScript constructor属性
- JavaScript constructor 属性
- constructor和prototype属性
- Test Case for JS
- 仿新浪微博的ListView下拉更新功能
- 安装tslib中遇到的错误:./autogen.sh: 4: autoreconf: not found
- 请放慢浮躁的脚步——毕业季的感悟
- listView下拉刷新(仿sina微博Android客户端效果)
- JS constructor属性
- ABAP--关于ABAP流程处理的一些命令的说明(stop,exit,return,check,reject)
- 十六周任务二
- 第十六周实验报告任务2
- Linux下利用O_DIRECT标志绕过VFS cache Tweet
- 超级牛人在华为工作十年的感悟
- openssl编程轻松入门(含完整示例)
- velocity包
- ecshop中ajax的调用原理