• JavaScript之对象


    创建对象的实例需要使用new关键字,就像java一样。
    访问属性:对象实例.属性名       或者        对象实例["属性名"]
    访问方法:对象实例.方法名


    自定义对象:其实就是方法,不过在实例化的时候加上new
    function Play(){}
    var  play = new Play();

    第一种创建对象的方法:
    function Play(){}
    var  play = new Play();
    play.width = 10;    //添加属性
    play.getWidth=function(){};    //添加方法


    第二种创建对象的方式:

    function fun(){
    var v = new Object()
    v.width = 300;
    v.getWidth = function(){this.width //访问width属性}
    return v;
    }

    var f = new fun();
    alert(f.width)

    第三种创建对象的方式:

    function fun1(){
    this.width = 300;
    this.getWidth = function(){this.width //访问width属性}
    }

    var f = new fun1();
    alert(f.width)
    //遍历属性,及其值
    for(var pro in f){
      alert(f[pro])  //值
    }

     

  • 相关阅读:
    javaee 第六周作业
    javaee 第五周作业
    javaee 第四周作业
    第三周作业
    第二周作业xml学习情况
    javaWeb 中http请求 get 与 post的区别
    第八周
    第七周
    第六周
    第五周
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2880136.html
Copyright © 2020-2023  润新知