zoukankan      html  css  js  c++  java
  • javascript对象

    一.类型

    五种基本类型:

      nullundefinednumberbooleanstring

      null表示没有声明,undefined表示声明后没有初始化,其余三个有对应的包装对象NumberBooleanString

    对象类型:

      object,比如常用的ArrayDateRegExp等,我们最常用的Function也是个对象

    二.类型识别函数

      验证类型  typeof 对象名;  输出对象类型

    三.javascript对象

      1.声明对象

      //用对象的方法声明对象

        var newobj = new Object();
        newobj.name = "jim";
        newobj.method = function(){
          console.warn(this.name);
        }
      //用字面值方法构造对象
        var obj = {
          name:"oliver",
          method:function(){
            console.warn(this.name);
          }
        }
      //使用方法来创建多个变量
        function createObj(name){
          var obj = {
            name:name,
            method:function(){
              console.warn("demo");
            }
          }
          return obj;
        }
      //定义构造方法
        function Person(name,age){
          this.name = name;
          this.age = age;
            this.method = function(){
            console.warn(this.constructor);//指向该对象的构造器function Person(name,age){}
            console.warn("new person");
            }
          //任何函数使用new运算符就是构造函数
        }

      2.对象间关系

      function Monkey(gender){

        this.gender = gender;
        this.method = function(){
          console.warn(this.gender);  
        }
      }

      
    function Person(name,age){
        this.name = name;
        this.age = age;
        this.prototype = new Monkey("male");//用另外一个构造函数赋值原型
        this.method = function(){
          console.warn(this.prototype);
        }
      }

      $(function(){
        $(".proto").click(function(){
          var p = new Person("jim",20);
          p.method();
        });
      });

  • 相关阅读:
    JavaScript-4(循环语句)
    JavaScript-3
    JavaScript-2(基本运算)
    JavaScript-1
    HTML基础-3(框架、表单与表格等)
    HTML基础-2(内容容器等)
    HTML基础-1(b,i,u命令等)
    【CSP-S2019模拟】10.07比赛总结
    JZOJ6380. 【NOIP2019模拟2019.10.06】小w与最长路(path)
    【CSP-S2019模拟】10.06比赛总结
  • 原文地址:https://www.cnblogs.com/brolanda/p/4503753.html
Copyright © 2011-2022 走看看