zoukankan      html  css  js  c++  java
  • 《JS中的面向对象技术》

    内容要点:

       1.什么是对象:JS权威指南学习总结-第六章 ,(有句话:一切都是对象)

       2.什么面向对象

            使用对象时,只关注对象提供的功能,不关注其内部细节,比如jQuery。面向对象是一种通用思想,并非只有编程中使用,任何事情都可以用。

      3.JS中面向对象三大特征: 封装、继承、多态

      4.JS自定义对象(三大对象两大属性):

            4.1创建对象方式:方法1:对象初始化的方法,就是通过对象直接量创建的对象。方法2:通过关键字new和构造函数的方法创建对象

            4.2对象属性定义:私有属性、对象属性、类属性

            4.3对象方法定义:私有方法、对象方法、类方法

            4.1创建对象的两种方法:

                   方法1:对象初始化的方法,就是通过对象直接量创建的对象。方法2:通过关键字new和构造函数的方法创建对象

                   方法1:通过对象初始化的方法创建对象:

                   var person={

                          name:"xingoo",  //对象的属性

                          age:26,

                          //对象的方法:如果函数挂载在一个对象上,作为对象的一个属性,就称它为对象的方法

                          say:function(){ console.log("say something"); },

                          //当通过这个对象来调用函数时,该对象就是此次调用的上下文,也就是该函数的this的值。

                          action:function(){ console.log("do something"); 

                           }                                                                 

                          };

                          console.log(person.name); //xinggo

            console.log(person.age); //26

            person.say();//say something

              person.action(); //do something

                 方法二.通过构造函数创建对象:

                       function student(name,age){      //用于初始化一个新创建的对象的函数称为构造函数
                              this.name = name;
                         this.age = age;
                         this.say = function(){
                             console.log("say something");
                                               }
                                this.action = function(){
                                       console.log("do something");
                                      }
                                }
                        var xingoo = new student("xingoo",27);  //关键字new做了两件事情:帮助创建一个空白对象,并返回了这个对象
                    console.log(xingoo.name); //xingoo
                    console.log(xingoo.age); //27
                    xingoo.say(); //say something
                    xingoo.action(); //do something

             4.2对象的属性:私有属性、对象属性、类属性;

                   对象属性需要创建对象后才能使用;

               私有属性在内部可以直接使用,在外部需要通过闭包才能使用。

               类属性可以通过对象名称直接使用。

                   function func(){

            this.objPro1 = "对象属性";

            func.prototype.objPro2 = "对象属性";

            var privatePro = "私有属性";

            }

            func.classPro = "类属性";

            console.log(func.classPro); //类属性

            var f = new func();

            console.log(f.objPro1);//对象属性

            console.log(f.objPro2);//对象属性

            <!-- 私有属性可以通过闭包获取 -->

              4.3对象的方法:私有方法、对象方法、类方法

                   function demoFunc1(){

            var privateFunc = function(){ console.log("this is privateFunc"); }; //私有方法

            privateFunc();

            this.objFunc1 = function(){ console.log("this is objFunc1"); };//对象方法

            demoFunc1.prototype.objFunc2 = function(){ console.log("this is objFunc2"); };

                          }

            demoFunc1.classFunc = function(){ console.log("this is classFunc"); };//类方法

            demoFunc1.classFunc(); //this is classFunc

            var f = new demoFunc1();//this is privateFunc

            f.objFunc1();//this is objFunc1

            f.objFunc2();//this is objFunc2

     5.JS中面向对象三大特征: 封装、继承、多态

         封装:不考虑内部实现,只考虑功能使用

        继承:从已有的对象上继承出新的对象。注:apply()实现属性和方法的继承(8.7函数的方法),prototype实现原型的继承

        多态:子类的方法会覆盖父类的方法,即表现出多态性。

        继承特性:  

           function Animal(name,age){

          this.name = name;

          this.age =age;

          this.say = function(){ console.log("animal say something"); }

        }

       function Cat(name,age){

          Animal.apply(this,[name,age]);

        }

          <!-- Cat.prototype = new Animal();-->

          var cat1 = new Cat("xingoo",3);

          console.log(cat1.name);//xingoo

          console.log(cat1.age); //3

          cat1.say(); //animal say something

                     console.log(Cat.prototype); //原型是:Cat{},_proto_:Cat

           如果开启注释的部分,可以发现,cat类的原型也变成了Animal。

        多态特性:

           子类的方法会覆盖父类的方法,即表现出多态性;注,在面向对象编程中,类B可以继承自另外一个类A,我们将A称为父类,将B称为子类。

           function Pig(name,age){

          this.say = function(){ console.log("i am pig"); }

        }

       Pig.prototype = new Animal();

       function Dog(name,age){

          this.say = function(){ console.log("i am dog"); }

        }

       Dog.prototype = new Animal();

      function say(animal){

          if(animal instanceof Animal){ animal.say(); }

        }

        var dog = new Dog();

        var pig = new Pig();

        say(dog); //i am dog

        say(pig); //i am Pig

  • 相关阅读:
    SQLServer.Produce的研究
    petshop以异步方式插入订单的疑惑(后面理解了)
    SQLHelper.GetCachedParameters方法之缓存
    DALFactory抽象工厂理解
    petshop异时消息处理队列抽象工厂
    AJAX学习2(经典)
    对SQLServerDAL.order的研究(不错,有心得)
    Invertory类对商品库存的更新困惑解决了
    AJAX学习1
    用DataSet读取xml文件
  • 原文地址:https://www.cnblogs.com/hanxuming/p/5851346.html
Copyright © 2011-2022 走看看