• 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");
  • 相关阅读:
    使用urllib
    spring常用的45个注解
    音痴
    android与JS函数传参遗留问题
    方舟编译器源码过一遍流程
    什么是语义学,解释器
    synchronized,ReentrantLock解决锁冲突,脏读的问题
    【Unity3d】ScrollRect自动定位到某点
    计算点到直线的距离】 C#实现
    理财-房月供占工资多少比较合适?
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6625298.html
Copyright © 2020-2023  润新知