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");
  • 相关阅读:
    java web项目防止多用户重复登录解决方案
    通过Google浏览器Cookie文件获取cookie信息,80以上版本有效
    js实现json数据导出为Excel下载到本地
    golang 搭建web服务器
    typescript笔记
    canvas屏幕动画
    canvas鼠标特效
    博客皮肤分享
    HTML的背景色和背景图、图片
    HTML表格头部、主体、页脚
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6625298.html
Copyright © 2011-2022 走看看