• JavaScript对象属性 constructor


     对象属性

      constructor 属性返回对创建此对象的数组函数的引用;

      constructor(构造函数) 在对象创建或实例化时候被调用的方法.通常使用该方法来初始化数据成员和所需资源。构造函数不能被继承;

    构造函数是一种特殊的方法,主要用来在创建对象时初始化对象,即为对象成员变量赋初始值.总与new运算符一起使用在创建对象语句中,特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同来区分它们,即构造函数的重载;

    <scirpt>
    
    var test = new Array();
    
    if (test.constructor == Array){
    
        document.write('This is an Array');      //test为数组返回当前
    
    }
    
    if (test.constructor == Boolean){
    
        document.write('This is an Boolean');     //test为布尔值则返回当前
    
    }
    
    if (test.constructor == Date){
    
        document.write('This is an Date');        //test为Data时间则返回当前
      
    }
    
    if (test.constructor == String){
    
        document.write('This is an String');     //test为字符串则返回当前
    
    }
    
    </script>
    

     

     在JavaScript中每个函数都有名为‘prototype'的属性,用于引用原型对象.此原型对象又有名为'constructor'的属性它反过来引用函数的本身这是一种循环使用

     function Animal(){} 
    
     function Person(){} 
    
    
     Person.prototype = new Animal(); 
     var person = new Person(); 
    
    
     alert(person.constructor);                               //Animal 
    

      

  • 相关阅读:
    【作业4】测试作业-兴趣问题清单
    【读后感3】高效程序员的45个习惯
    【作业3】关于C语言的问卷调查
    【作业2】价值观作业
    Spring的零配置
    Spring容器中bean的作用域
    Spring注入方式
    Spring整合Struts2
    my first go
    Struts2对ajax的支持
  • 原文地址:https://www.cnblogs.com/liang1/p/5303425.html
Copyright © 2020-2023  润新知