JavaScript closure: compare with Java

Resume

Closure in JavaScript provides us an opportunity to make our codes clearer and Object Oriented. Actually I consider that closure is an implementation of class in JavaScript. We will compare with Java in this passage.


Simple inheritance in Java

In Java, we can easily write 2 class like

//Product.class
class Product{
    protected String name;
    protected int price;
    public Product(String name, int price){
        this.name = name;
        this.price = price;
        if (price < 0) {
            throw new RangeError('Cannot create product ' + this.name + ' with a negative price'); //RangeError is not defined, because it's not important in this case.
        }
    }
}
//Food.class
class Food extends Product{
    protected String category = "food";
    public Food(String name, int price){
        super(name, price);
    }
}

Food inherits Product and initiates Product with 2 arguments: name and price.
It’s quite simple so that we will not explain it.
Let’s see what should we do in JavaScript.


JavaScript

function Product(name, price) {
    this.name = name;
    this.price = price;
    if (price < 0) {
        throw RangeError('Cannot create product ' + this.name + ' with a negative price');
    }
}

function Food(name, price) {
    Product.call(this, name, price);
    this.category = 'food';
}

console.log((new Food('Cake', 5)).price);

The most import thing here is function.call(this argument, argument, argument, ...).
When we run these codes, it will print 5 which is exactly the price of cheese.
But why?
When we instantiate Food, then we jump into the function Food(name, price), then we invoke the function Product.call(this, name, price) and pass this to Product(name, price). In Product(name, price), actually we are modifying Food, because we passed this in Product.call(this, name, price). So that this points to object Food. And we assign name and price to this(which is the reference of Food).
So, it’s the object Food which is being modified and finally we have name, price and category in Food.


Some people may say that we may implement like this:

function Product(self, name, price) {
    self.name = name;
    self.price = price;
    console.log(self);
    if (price < 0) {
        throw RangeError('Cannot create product ' + self.name + ' with a negative price');
    }
}

function Food(name, price) {
    Product(this, [name, price]);
    console.log(this.price);
    this.category = 'food';
}

console.log((new Food('Cake', 5)).price);

But actually it won’t work. Because JavaScript is a little bit different. Even though Product and Food are 2 objects. But when we pass arguments to this 2 “functions”, it will make a copy of the parameter that we passed, not the reference.
So that it’s in vain to do so.

If somebody have another implementation, please leave it in comment area.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值