JavaScript closure: compare with Java
来源:互联网 发布:centos 查看软件版本 编辑:程序博客网 时间:2024/04/30 15:25
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.classclass 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.classclass 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.
- JavaScript closure: compare with Java
- Compare javascript strings with
- JavaScript - Closure
- Javascript Closure
- javascript Closure
- JavaScript Closure
- Javascript Closure
- javascript closure
- JavaScript closure
- javascript closure
- [JavaScript] Closure In JavaScript
- compare signed with unsigned
- compare with zero value
- sipXecs compare with Asterisk
- compare with two XML
- debug compare with release
- JXL compare with POI
- 诡异的JavaScript Closure
- 整数除法对照表(变量/2的幂)
- POJ 3026
- 徐海蛟:深度学习与计算机视觉理论与实验2
- POJ 1251
- POJ 1679
- JavaScript closure: compare with Java
- 计算着色器
- 第一天
- Leetcode 177. Nth Highest Salary
- Leetcode 178. Rank Scores
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(一)Spring Boot介绍
- 勾股定理一日一证连载88
- Tracking your manuscript status in journal
- 占位