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

    1.  直接创建实例:

       //简单对象
            var person1 = new Object();
            person1.name = "Mike";
            person1.age = 29;
            person1.job = "Software Engineer";
            person1.sayName = function () {
                document.write(this.name+"</br>");
            }
            person1.sayName();
    
            //对象字面量
            var person2 = {
                name: "yoyo",
                age: 29,
                job: "Software Engineer",
                sayName: function () {
                    document.write(this.name + "</br>");
                }
            }
            person2.sayName();

    2. 使用对象构造器

      //工厂模式
            //用函数来包装
            function creatPerson(name,age,job)
            {
                var person = new Object();
                person.name = name;
                person.age = age;
                person.job = job;
                person.sayName = function () {
                    document.write(this.name + "</br>");
                }
                return person;
            }
            var person3 = creatPerson("tom", 21, "baidu");
            person3.sayName();
    
            //构造函数模型
            function Person(name, age, job) {
                this.name = name;
                this.age = age;
                this.job = job;
                //第一种写法,队友带参数的方法如下
                this.sayName = function (name) {
                    this.name = name;
                    document.write(this.name + "</br>");
                }
                //第二种写法
                //this.sayName = new Function(" document.write(this.name + '</br>')");
            }
            var person4 = new Person("tony", 31, "tencent");
            person4.sayName("yoyo");

    3. 把方法添加到JavaScript对象

            //把方法添加到JavaScript对象
            function People(firstname,lastname,age,eyecolor)
            {
                this.firstname = firstname;
                this.lastname = lastname;
                this.age = age;
                this.eyecolor = eyecolor;
                this.displayName = displayName;
                this.changeName = changeName;
                function displayName()
                {
                    document.write(this.firstname + " " + this.lastname + "</br>");
                }
                //同一个方法名不支持重载,如果写了重载,那么后面的会覆盖前面的
                function changeName(fname)
                {
                    this.firstname = fname;
                    document.write(this.firstname + " " + this.lastname + "</br>");
                }
                function changeName(fname,lname)
                {
                    this.firstname = fname;
                    this.lastname = lname;
                    document.write(this.firstname + " " + this.lastname + "</br>");
                }
            }
            var people1 = new People("Steve", "Jobs", 56, "blue");
            people1.displayName();
            people1.changeName("Ballmer");//这个会调用两个参数的方法
            people1.changeName("Ballmer","Jin");
  • 相关阅读:
    自己动手编写一个网络图片爬虫
    使用maven构建项目的注意事项
    maven 构建一个web项目
    构建简单的Maven工程,使用测试驱动的方式开发项目
    像Maven一样构建java项目的目录,更好的管理java工程的源码
    Tomcat源码导入eclipse的步骤
    [HNOI2012] 矿场搭建
    UVA10641 Barisal Stadium 照亮体育馆
    CSP-J/S(NOIP PJ/TG) 游记
    [BalticOI 2011 Day1] Switch the Lamp On
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6625298.html
Copyright © 2011-2022 走看看