Difference between prototype and this in JavaScript
来源:互联网 发布:centos 查看软件版本 编辑:程序博客网 时间:2024/04/30 10:15
Resume
When I’m studying JavaScript, I got a little bit confused while I first time look at function.prototype
(Here, function
points to a certain function, not Function
). In this passage, we will see the difference between prototype
and this
.
this
First of all, we propose an example.
var func = function(){ this.get = function(){console.log('A')};}func.prototype.updateGet = function(){ this.get = function(){console.log('B')};}var func0 = new func(), func1 = new func();func0.get();func1.get();func0.updateGet();func0.get();func1.get();
And the result will be
AABA
as we expected.
Because, when we invoke updateGet()
, it has just updated the function get()
in object func0
. So that func1
will not be affected.
function.prototype
var func = function(){}func.prototype.get = function(){console.log('A')};func.prototype.updateGet = function(){ func.prototype.get = function(){console.log('B')};}var func0 = new func(), func1 = new func();func0.get();func1.get();func0.updateGet();func0.get();func1.get();
And the result will be
AABB
Because when we modify functions in func.prototype
, it means that we modify every instance depends on function in prototype.
As prototype
means the prototype of a function, I believe it’s pretty fair to do so.
Reference: http://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript
this > function.prototype
Moreover, as I have tested, when we do
var func = function(){this.get = function(){console.log('C')};}//We define get() here.func.prototype.get = function(){console.log('A')};func.prototype.updateGet = function(){ func.prototype.get = function(){console.log('B')};}var func0 = new func(), func1 = new func();func0.get();func1.get();func0.updateGet();func0.get();func1.get();
It will return
CCCC
which makes me consider that this
has higher priority than function.prototype
.
Function.prototype
As all function “inherit” from Function
, when we add methods to Function.prototype
, it will add a method to all functions.
Function.prototype.test = function(){ return 10;}var func = function(){}func.test();
Codes above will show
10
as we expected.
References
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
http://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript
- Difference between prototype and this in JavaScript
- Difference between Properties and Attributes in JavaScript
- Difference between STA and MTA in thread
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Difference between __str__ and __repr__ in Python
- Difference Between Thread and AsyncTask in Android
- Difference between Comparator and Comparable in java
- Difference between Stack and Heap in Java
- Difference between dialog and session in SIP?
- Difference between ArrayList and Vector In java
- Difference between Controller and Model in MVC
- Difference between datacontract and messagecontract in wcf
- 【Struts0002】Difference between # , $ and % signs in Struts2
- Difference between HashMap and Hashtable in Java
- matlab多项式操作
- page jump
- matlab仿真实验模拟
- matlab公式符号计算推倒
- Android studio 运行失败:Installation failed since the APK was either not signed, or signed incorrectly.
- Difference between prototype and this in JavaScript
- Bayes_Game
- android抓包工具fiddler抓包使用方法
- eclipse如何点击png或者jpg格式图片就能预览
- BZOJ 4612([Wf2016]Forever Young-二分)
- BZOJ 3293/1465/1045([Cqoi2011]分金币/糖果传递/[HAOI2008] 糖果传递-列方程)
- [问题解答]CCF:201509-3:模板生成系统
- POJ 1258
- POJ 1751