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
  • 下载

获取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类似。

Review pat1055 compare cin with scanf,etc..

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

第二章 Closure javascript注解

Closure Library原始代码都有注释,其中一些都有特殊的格式,并被Cloure Compiler处理。理解这些注解对阅读Closure代码有很大帮助,本书将有这些例子。本章介绍的JSDoc标...

学习Javascript闭包(Closure)

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

Javascript中闭包(Closure)

参考资料:  http://baike.baidu.com/view/648413.htm http://www.jb51.net/article/24101.htm http://www....

深入理解Javascript闭包(closure)

一、什么是闭包?    “官方”的解释是:所谓“闭包”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分。   相信很少有人能直接看...

深入理解JavaScript闭包(closure)

最近在网上查阅了不少Javascript闭包(closure)相关的资料,写的大多是非常的学术和专业。对于初学者来说别说理解闭包了,就连文字叙述都很难看懂。撰写此文的目的就是用最通俗的文字揭开Java...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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