JavaScript closure: compare with Java

原创 2016年08月31日 04:57:54

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.

版权声明:本文章禁止商业使用,非商用转载必须注明出处和作者。作者保留一切权利。

Google Javascript Closure Compiler

  • 2017年05月27日 22:42
  • 6.28MB
  • 下载

closure library 模块化的javascript库

  • 2015年12月21日 23:54
  • 5.46MB
  • 下载

获取MySQL加密密码并验证用户输入(登录界面),retrieve hashpwd from mysql and compare with input password on login

获取MySQL加密密码并验证用户输入(登录界面),retrieve hashpwd from mysql and compare with input password on login...

PyCharm使用技巧:Compare With(文件比较工具)

PyCharm的Compare With提供了文件比较功能,包括比较文件夹和比较文件,功能和文件比较工具beyond compare类似。...
  • xiemanR
  • xiemanR
  • 2017年06月17日 13:07
  • 2375

Review pat1055 compare cin with scanf,etc..

Now that we prefer to ,why we use in some acm

Javascript中闭包(closure)的理解与浅析

最近在网上查阅了不少Javascript闭包(closure)相关的资料,写的大多是非常的学术和专业。对于初学者来说别说理解闭包了,就连文字叙述都很难看懂。撰写此文的目的就是用最通俗的文字揭开Java...

学习Javascript闭包(Closure)

闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。下面就是我的学习笔记,对于Javascript初学者应该是很有用的。 一、变量的作用域     ...

学习Javascript闭包(Closure)

在javascript中,子函数可以读取父函数中的变量,而父函数却无法读取子函数中的变量,就像是儿子可以花父亲的钱,而父亲却不能花儿子的钱。而闭包要做的就是把儿子的钱拿出来给父亲花。 闭包通过在...
  • sir1241
  • sir1241
  • 2016年10月11日 17:18
  • 106

闭包:学习Javascript闭包(Closure)

http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html?20120612141317#comments 学...
  • wowotuo
  • wowotuo
  • 2015年01月18日 21:00
  • 537

Javascript闭包(Closure)代码详解

闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。很早就接触过闭包这个概念了,但是一直糊里糊涂的,没有能够弄明白JavaScript的闭包到底是什...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JavaScript closure: compare with Java
举报原因:
原因补充:

(最多只允许输入30个字)