首页 > Web开发 > 详细

js的封装、继承与多态

时间:2019-05-13 12:17:38      阅读:91      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>test</title>
    </head>
    <body>
        <script>
            window.onload = function() {
                // 封装
                var Book = function(id, name, price) {
                    // 私有变量(在函数内部定义,函数外部访问不到,实例化之后实例化的对象访问不到)
                    var num = 1
                    var id = id

                    function privateFunction() {
                        console.log(this is private)
                    }
                    // protected(可以访问到函数内部的私有属性和私有方法,在实例化之后就可以对实例化的类进行初始化拿到函数的私有属性)
                    this.getNum = function() {
                        console.log(num)
                    }
                    this.getFunction = function() {
                        privateFunction()
                    }
                    //public(实例化的之后,实例化的对象就可以访问到了~)
                    this.name = name
                    this.copy = function() {
                        console.log(this is public)
                    }
                }
                //在Book的原型上添加的方法实例化之后可以被实例化对象继承
                Book.prototype.proFunction = function() {
                    console.log(this is proFunction)
                }
                //在函数外部通过.语法创建的属性和方法,只能通过该类访问,实例化对象访问不到
                Book.setTime = function() {
                    console.log(this is new time)
                }
                var book1 = new Book(B11, 悲惨世界, $99)
                // 通过this创建的公共属性和方法,实例化的时候会复制一遍,所以可以访问到
                console.log(book1.name)
                book1.copy()
                // 通过protected的getNum来访问Book的私有变量
                book1.getNum()
                book1.getFunction()
                // 直接通过实例来访问私有变量是无法访问的
                // console.log(book1.num)
                // book1.privateFunction()
                // 通过prototype创建的方法可以在实例中访问
                book1.proFunction()
                // 直接在构造函数中.的方法实例是无法访问的
                // book1.setTime()
                // 只能通过构造函数来访问
                Book.setTime()
                // privateFunction是无法访问的
                // Book.privateFunction()

                // 继承
                var SuperClass = function() {
                    var id = 1
                    this.name = [javascript]
                    this.superValue = function() {
                        console.log(superValue is true)
                        console.log(id)
                    }
                }
                SuperClass.prototype.getSuperValue = function() {
                    return this.superValue()
                }
                var SubClass = function() {
                    this.subValue = function() {
                        console.log(this is subValue )
                    }
                }
                //继承父类
                SubClass.prototype = new SuperClass()
                SubClass.prototype.getSubValue = function() {
                    return this.subValue()
                }
                var sub = new SubClass()
                var sub2 = new SubClass()
                console.log(sub)

                // 多态
                function Add() {
                    function zero() {
                        return 0
                    }

                    function one(id) {
                        return 0 + id
                    }

                    function two(id, name) {
                        return 0 + id + name
                    }
                    this.print = function() {
                        var arg = arguments
                        var len = arg.length
                        switch(len) {
                            case 0: {
                                return zero()
                            }
                            case 1: {
                                return one(arg[0])
                            }
                            case 2: {
                                return two(arg[0], arg[1])
                            }
                        }
                    }
                }
                var add = new Add()
                console.log(add.print())
                console.log(add.print(1))
                console.log(add.print(1, 2))
            }
        </script>
    </body>
</html>

 

js的封装、继承与多态

原文:https://www.cnblogs.com/tllw/p/10855297.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!