JavaScript_对象

Posted HepburnXiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");

 

以上是关于JavaScript_对象的主要内容,如果未能解决你的问题,请参考以下文章

Atom编辑器折腾记_(15)JS代码片段补全(插件:javascript-snippets)

常用Javascript代码片段集锦

VSCode自定义代码片段——JS中的面向对象编程

JavaScript单行代码,也就是代码片段

VSCode自定义代码片段9——JS中的面向对象编程

从 TypeScript 类型定义或 GraphQL 片段定义生成 JavaScript 空对象