JS面向对象高级特性

简介:
+关注继续查看

本篇是通过学习视频《一头扎进javascirpt高级篇》整理的一些相关知识,大致包括下面几个方面:

  1 对象的创建方法

  2 对象的对象属性、私有属性、类属性

  3 对象的对象方法、私有方法、类方法

  4 javascirpt的继承、封装、与多态

  对象的创建方法:

  对象的创建可以通过两种方式,第一种通过对象初始化的方法:

            var person={
                name:"xingoo",
                age:26,
                say:function(){
                    console.log("say something");
                },
                action:function(){
                    console.log("do something");
                }
            };

            console.log(person.name);
            console.log(person.age);
            person.say();
            person.action();

  第二种方式通过构造函数创建:

            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);
            console.log(xingoo.name);
            console.log(xingoo.age);
            xingoo.say();
            xingoo.action();

  对象的属性

  对象的属性分为对象属性、私有属性和类属性。

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

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

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

       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);

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

 

  对象的方法

  对象方法包括:对象方法,私有方法和类方法,使用类似前面的属性。

            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();

            var f = new demoFunc1();
            f.objFunc1();
            f.objFunc2();

  继承、封装与多态

  JS要想实现继承,需要通过apply方法或者prototype实现。

  如果单纯的使用apply方法,子类的原型是子类;如果使用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);
            console.log(cat1.age);
            cat1.say();

  上面代码中,cat的原型是cat;

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

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

            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);
            say(pig);

  使用到的全部代码:

<!doctype html>
<html>
    <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <script type="text/javascript">
            <!-- 对象初始化器方式 -->
            var person={
                name:"xingoo",
                age:26,
                say:function(){
                    console.log("say something");
                },
                action:function(){
                    console.log("do something");
                }
            };

            console.log(person.name);
            console.log(person.age);
            person.say();
            person.action();

            <!-- 构造函数方式 -->
            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);
            console.log(xingoo.name);
            console.log(xingoo.age);
            xingoo.say();
            xingoo.action();

            <!-- 对象属性 私有属性,对象属性,类属性 -->
            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);

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

            <!-- 私有方法,对象方法,类方法 -->
            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();

            var f = new demoFunc1();
            f.objFunc1();
            f.objFunc2();

            <!-- 封装性,继承性,多态性 -->
            <!-- apply()实现属性和方法的集成,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);
            console.log(cat1.age);
            cat1.say();

            <!-- 继承 -->
            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);
            say(pig);
        </script>
    </body>
</html>

  运行结果:

本文转自博客园xingoo的博客,原文链接:JS面向对象高级特性,如需转载请自行联系原博主。
相关文章
|
22天前
|
JSON 编译器 Go
go语言特性--反射 青训营
go语言特性--反射 青训营
|
2月前
|
Java 程序员 开发者
初识Java,谈一谈面相对象编程的基本特性
面向对象编程(Object-Oriented Programming,简称 OOP),是Java这门编程语言最基本,也是最核心的思想,它是Java程序员必须熟练掌握的编程范式之一,深入理解并且灵活运用其基本特性,可以在实际开发过程中达到事半功倍的效果,也是Java开发者进阶路上的必经之路。
|
2月前
|
Go 开发者
一文详解Go语言接口嵌套组合的精髓!
一文详解Go语言接口嵌套组合的精髓!
30 0
|
2月前
|
设计模式 Go 数据安全/隐私保护
这些技巧你必须知道,Go语言模拟继承顶级指南
这些技巧你必须知道,Go语言模拟继承顶级指南
20 0
|
2月前
|
Go
深度解析:Go语言面向对象编程和方法调用机制
深度解析:Go语言面向对象编程和方法调用机制
17 0
|
4月前
|
设计模式 JavaScript 前端开发
js面向对象入门
js面向对象入门
40 0
|
10月前
|
设计模式 Rust JavaScript
Java面向对象的一些特性
Java面向对象的一些特性
|
安全 Java
java面向对象三大特性,封装篇
1.封装的概念 在面向对象程式设计方法中,封装是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法。 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问。
68 0
|
Java Go API
聊一聊Go语言的反射机制
聊一聊Go语言的反射机制
|
分布式计算 大数据 Serverless
高级特性_闭包_概念 | 学习笔记
快速学习 高级特性_闭包_概念
43 0
高级特性_闭包_概念 | 学习笔记
相关产品
云迁移中心
推荐文章
更多