美文网首页
对象创建的方法

对象创建的方法

作者: HonneyHao | 来源:发表于2019-02-02 11:42 被阅读0次
    js创建对象的方式 

    <!DOCTYPE html>

    <html>

        <head>

            <meta charset="UTF-8">

            <title></title>

        </head>

        <!--

            封装:将数据和数据的操作方法集中在一起

            继成:一个类型的对象可以访问其父类的属性和方法

            多态:同一种方法作用于不同的对象,会有不同的结果

        -->

        <body>

            <!--

                对象创建的方法:

            -->

            <script type="text/javascript">

                /*

                    字面量创建方式

                    1.此法基本不用,因为会造成代码冗余(缺点)

                 */

                    var obj = {

                        name : "asd",

                        height:185,

                        sayHello:function(){

                            console.log(this.name);

                        }

                    }

                    obj.sayHello();

                /*

                    2.利用构造函数创建对象

                        构造函数也是函数,必须用 new 运算符来调用,否则就是普通函数

                 * */

                    function foo(name,age){

                        this.name = name;

                        this.age = age;

                        this.sayHello=function(){

                            console.log(this.name);

                        }

                    }

                    var dog = new foo("gou",25);

                    var cat = new foo("mao",25);

                    console.log(dog.sayHello == cat.sayHello);//false

                    //可见:结果为false,说明用构造函数创建对象,每创建一个对象,都会创建一个新的相同的方法,会增加内存的开销(缺点)

                    console.log(dog.sayHello() == cat.sayHello());//true

                    console.log(dog.sayHello,dog.sayHello());

                    //dog.sayHello在传给具体的对象之后,因为对象地址不同,所以不相等了

                    //dog.sayHello()是一个值,在这里,只是相等的,所以结果是true

                /*

                    3.工厂模式,创建出来的对象都是一样的,例如:

                 * */

                    function createObject(name,age){

                        var obj = new Object();

                        obj.name = name;

                        obj.age = age;

                        obj.sayHello = function(){

                            console.log(this.name);

                        }

                        return obj;                 //注意这句,return创建的对象

                    }

                    var yh = new createObject("yh",20);

                    var wcn = new createObject("wcn",20);

                    console.log(yh instanceof Object);

                    console.log(wcn instanceof Object);

                    console.log(typeof yh);

                    //缺点:无法判断对象的类型(比如Person类,Animal类)

                /*

                    4.利用原型来创建对象( 把属性都挂在在原型上)

                        a.每一个构造函数都有一个原型对象

                        b.每一个原型对象都有一个指向构造函数的指针

                        c.每一个实例都有指向原型对象的内部指针

                        d.原型对象上的方法和属性能被实例所访问

                 * */

                    function Person(){

                    }

                    Person.prototype.name = "john";

                    Person.prototype.age = 20;

                    Person.prototype.sayHello = function(){

                        console.log(this.name);

                    }

                    var person1 = new Person();

                    var person2 = new Person();

                    console.log(person1.sayHello == person2.sayHello);//true

                    //可见,利用原型创建对象不会重复生成相同的对象,但是,创建的属性也是一样的。即它是无法传参的

                /*5.组合创建(构造函数和原型对象的组合):

                    把属性放在构造函数(function foo(){})里,把方法放在原型对象上(prototype)

                    优点:

                        1.避免了利用原型创建对象需要太多的声明(每个属性,每个方法);

                        2.避免了工厂模式下,每次产生新对象都要创建新的,相同的方法,增加内存开销

                */

                /*如下:

                    构造一个person类,他有几个属性(放在构造函数(function foo(){})里),

                    和几个方法(放在原型对象上(prototype)).

                    开始:

                    * */

                //属性

                function Person(name,age,hei){//注意类名开头大写

                    this.name = name;

                    this.age = age;

                    this.hei = hei;

                }

                //方法

                Person.prototype.sayHello = function(){//声明原型对象的方法

                    console.log("我叫"+this.name+",我今年"+this.age+"岁")

                }

                //生成对象(实例):

                var yh = new Person("尹豪",20,185);

                yh.sayHello();                      //对象(实例)调用了自己的对象

                console.log(yh instanceof Person);  //可以检验yh是属于Person类的

                /*

                6.ES6的创建方法

                 * */

                class Person{

                    constructor(name,age,height){       //属性构造

                        this.name = name;

                        this.age = age;

                        this.height = height

                    }

                    say(){

                        console.log(this.name);         //方法

                    }

                    fly(){

                        console.log(this.height);           //方法

                    }

                }

                var p1 = new Person(name,age,height);

            </script>

        </body>

    </html>

    相关文章

      网友评论

          本文标题:对象创建的方法

          本文链接:https://www.haomeiwen.com/subject/ieqxsqtx.html