• this关键字在函数中的应用


    this 关键字用来将方法和属性绑定到一个对象的实例上

    <script type="text/javascript">
        /*  检查银行账户对象的构造函数*/
        function Checking(amount) {
            this.balance = amount; // 属性
            this.deposit = deposit; // 方法
            this.withdraw = withdraw; // 方法
            this.toString = toString; // 方法
        }
        function deposit(amount) {
            this.balance += amount;
        }
        function withdraw(amount) {
            if (amount <= this.balance) {
                this.balance -= amount;
            }
            if (amount > this.balance) {
                alert("Insufficient funds");
            }
        }
        function toString() {
            return "Balance: " + this.balance;
        }
        var account = new Checking(500);
        account.deposit(1000);
        alert(account.toString()); //Balance: 1500
        account.withdraw(750);
        alert(account.toString()); // 余额: 750
        account.withdraw(800); // 显示 " 余额不足 "
        alert(account.toString()); // 余额: 750
    </script>
  • 相关阅读:
    Redis-持久化
    Redis-Sort命令
    Redis-ZSet常用命令
    Redis-Hash
    Redis-Set常用命令
    Redis-List常用命令
    Redis-String常用命令
    访问控制
    c++之旅:多态
    c++之旅:类型的强制转换
  • 原文地址:https://www.cnblogs.com/feile/p/5369598.html
Copyright © 2020-2023  润新知