zoukankan      html  css  js  c++  java
  • day04—JavaScript之面向对象

    转行学开发,代码100天——2018-03-20

    对象是变量的容器,是键值对的容器,也是属性和方法的容器。

    万物接对象

    1.JavaScript中对象定义的方式

    方法一:new Object()

    //1.new Object()方式
        people =new Object();
        people.name="Allen";
        people.age="28";
        people.work="Engeneer";
    
        document.write("name:"+people.name+",age:"+people.age+",work:"+people.work);

    方法二:{键值对} 

    多个键值对之间用,隔开
    //2.{键值对}  多个键值对之间用,隔开
        var people=    {
            name:"Allen",
            age:"28",
            work:"Engeneer"
        };
    
        document.write("name:"+people.name+",age:"+people.age+",work:"+people.work);

     方法三:函数方法。通过定义函数创建对象。

    //3.函数方式创建对象
        function people(name,age)
        {
            this._name = name;
            this._age = age;
    
        }
        al = new people("Allen","28");
        document.write("name:"+al._name+",age:"+al._age);

    2、对象属性的调用

    对象属性的调用方式有两种,即   .property 或 ["property"]

    people.name或
    people["name"]

     3、对象方法作为一个函数定义存储在对象属性中,

    //对象方法作为一个函数定义存储在对象属性中
        var people =
        {
            name:"Allen",
            age:"28",
            work:"Engeneer",
            fullInfo:function()
            {
                return this.name+this.age+this.work;
            }
        }
    
        document.write(people.fullInfo());
    心未老,即奋不顾身!
  • 相关阅读:
    AtCoder Regular Contest 093
    AtCoder Regular Contest 094
    G. Gangsters in Central City
    HGOI 20190711 题解
    HGOI20190710 题解
    HGOI 20190709 题解
    HGOI 20190708 题解
    HGOI20190707 题解
    HGOI20190706 题解
    HGOI 20190705 题解
  • 原文地址:https://www.cnblogs.com/allencxw/p/8613405.html
Copyright © 2011-2022 走看看