Javascript面向对象编程--闭包

参考书籍:JavaScript设计模式

将类的静态变量通过闭包来实现
js

var Book = (function () {
    console.log('页面加载时执行');
    console.log('Book this指向window + bookNum: ' + bookNum);
    console.log(this);
    //静态私有属性
    var bookNum = 0;
    //静态私有方法
    function checkBook(name) {
        console.log('this is static private function');
    }
    //返回构造函数
    return function (newId, newName, newPrice) {
        console.log('方法触发时执行');
        console.log('constructor this指向构造函数  + bookNum: ' + bookNum);
        console.log(this);
        //私有属性
        var name, price;
        //私有方法
        function checkId() { };
        //特权方法
        this.getName = function () {
            return this.name;
        };
        this.getPrice = function () {
            return this.price;
        };
        this.setName = function () {
            this.name = newName;
        };
        this.setPrice = function () {
            this.price = newPrice;
        };
        //公有属性
        this.id = newId;
        //公有方法
        this.copy = function () {
            console.log('this is copy function');
        };
        bookNum++;
        if (bookNum > 100) {
            throw new Error('我们仅出版100本书');
        }
        //构造器
        this.setName(newName);
        this.setPrice(newPrice);
        console.log('end constructor this指向构造函数 + bookNum: ' + bookNum);
        console.log(this);
    }
})();
Book.prototype = {
    //静态公有属性
    isJSbook: false,
    //类静态公有方法
    display: function () {
        console.log('book display');
    }
}
function displayBook() {
    console.log('displayBook this指向window');
    console.log(this);
    var book = new Book(10, 'javascript', 50);
    document.getElementById("demo").innerHTML = book.getName();
    console.log('book.price: ' + book.price);
    console.log('book.bookNum: ' + book.bookNum);
}

console
在这里插入图片描述
闭包是有权访问另一个函数作用域中变量的函数,即在一个函数内部创建另一个函数。我们将这个闭包作为创建对象的构造函数,这样它即使闭包又是可实例对象的函数,可访问到类函数作用域中的变量,如bookNum.
上面的例子在闭包外部添加原型属性和方法,看上去像似脱离了闭包这个类,所以有时候在闭包内部实现一个完整的类然后将其返回,如下:

var Book = (function () {
    console.log('页面加载时执行');
    console.log('Book this指向window + bookNum: ' + bookNum);
    console.log(this);
    //静态私有属性
    var bookNum = 0;
    //静态私有方法
    function checkBook(name) {
        console.log('this is static private function');
    }
    //创建类
    function _book(newId, newName, newPrice) {
        console.log('方法触发时执行');
        console.log('constructor this指向构造函数  + bookNum: ' + bookNum);
        console.log(this);
        //私有属性
        var name, price;
        //私有方法
        function checkId() { };
        //特权方法
        this.getName = function () {
            return this.name;
        };
        this.getPrice = function () {
            return this.price;
        };
        this.setName = function () {
            this.name = newName;
        };
        this.setPrice = function () {
            this.price = newPrice;
        };
        //公有属性
        this.id = newId;
        //公有方法
        this.copy = function () {
            console.log('this is copy function');
        };
        bookNum++;
        if (bookNum > 100) {
            throw new Error('我们仅出版100本书');
        }
        //构建原型
        this.setName(newName);
        this.setPrice(newPrice);
        console.log('end constructor this指向构造函数 + bookNum: ' + bookNum);
        console.log(this);
    }
    _book.prototype = {
        //静态公有属性
        isJSbook: false,
        //类静态公有方法
        display: function () {
            console.log('book display');
        }
    }
    //返回类
    return _book;

})();
function displayBook() {
    console.log('displayBook this指向window');
    console.log(this);
    var book = new Book(10, 'javascript', 50);
    document.getElementById("demo").innerHTML = book.getName();
    console.log('book.price: ' + book.price);
    console.log('book.bookNum: ' + book.bookNum);
}

console
在这里插入图片描述
类的3个部分,第一部分是构造函数内的,这是供实例化对象复制用的,第二部分是构造函数外的,直接通过点语法添加的,这是供类使用的,实例化对象是访问不到的,第三部分是类原型中的,实例化对象可以通过其原型链间接的访问到,也是为供所有实例化对象所共用的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值