js基础篇2

1.闭包和面向对象设计

1.1闭包实现

		var extent = function(){
            var value = 0;
            return {
                call:function(){
                    value++;
                    console.log(value)
                }
            }
        }

        var extent = extent()
        extent.call() // 输出1
        extent.call() // 输出2
        extent.call() // 输出3

1.2面向对象

       var extent = {
            value:0,
            call:function(){
                this.value++;
                console.log(this.value)
            }
        }
        
        extent.call() // 输出1
        extent.call() // 输出2
        extent.call() // 输出3

1.3或者面向对象

		var Extent = function(){
            this.value = 0
        }
        Extent.prototype.call = function(){
            this.value++;
            console.log(this.value)
        }
        var extent = new Extent()
        extent.call() // 输出1
        extent.call() // 输出2
        extent.call() // 输出3

2.命令模式

2.1使用面向对象方式实现

    <button id="execute">点击我执行命令</button>
    <button id="undo">点击我执行命令</button>
    <script>
        var Tv = {
            open:function(){
                console.log('打开电视机')
            },
            close:function(){
                console.log('关闭电视机')
            }
        }
        var OpenTvCommand = function(receive){
            this.receive = receive;
        }
        OpenTvCommand.prototype.execute = function(){
            this.receive.open()
        }
        OpenTvCommand.prototype.undo = function(){
            this.receive.close()
        }
        var setcommand = function(command) {
            document.getElementById('execute').onclick = function(){
                command.execute()
            }
            document.getElementById('undo').onclick = function(){
                command.undo()
            }
        }
        setcommand(new OpenTvCommand(Tv))
    </script>

2.2使用闭包方式实现

    <button id="execute">点击我执行命令</button>
    <button id="undo">点击我执行命令</button>
    <script>
        var Tv = {
            open:function(){
                console.log('打开电视机')
            },
            close:function(){
                console.log('关闭电视机')
            }
        }
        var createCommand = function(receive){
            var execute = function(){
                return receive.open()
            }
            var undo = function(){
                return receive.close()
            }
            return {
                execute:execute,
                undo:undo
            }
        }
        var setcommand = function(command) {
            document.getElementById('execute').onclick = function(){
                command.execute()
            }
            document.getElementById('undo').onclick = function(){
                command.undo()
            }
        }
        setcommand( createCommand(Tv) )
    </script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值