Difference between prototype and this in JavaScript

原创 2016年08月31日 02:28:59

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

A
A
B
A

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

A
A
B
B

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

C
C
C
C

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

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

相关文章推荐

What's the difference between “local” and “my” in Perl

There are two kinds of variable scopes in Perl: Global variables: They reside in the current pa...

Difference between start and run method in Thread – Java Tutorial

Difference between start and run method in Thread – Java Tutorial Why do one call start method of t...

Difference between Externalizable and Serializable in Java

One obvious difference that Serializable is a marker interface and doesn't contain any methods whe...

The difference between EXISTS and IN upon Oracle

The process flow of EXISTS like as below: select * from t1 where exists ( select null from t2 where...

Difference between MPDU, MSDU, AMPDU, and AMSDU in 802.11n

原文地址:http://ergodicthoughts.blogspot.com/2012/02/difference-between-mpdu-msdu-ampdu-and.html   ...
  • zxygww
  • zxygww
  • 2016-03-11 11:46
  • 1328

python中下划线,双下划线,首尾双下划线的区别(Difference between _, __ and __xx__ in Python)

Difference between _, __ and __xx__ in Python When learning Python many people don't really under...
  • Sinchb
  • Sinchb
  • 2013-03-13 13:24
  • 2762

In SQL, what is the difference between a left join and a left outer join?

There is actually no difference between a left join and a left outer join – they both refer to the e...

In SQL, what’s the difference between a full join and an inner join?

A brief explanation of a joinLet’s start with a quick explanation of a join. Joins are used to combi...

Difference between Vector and ArrayList in java?

面试的时候也总是问的一个问题,说一下Vector与ArrayList的区别? 这个问题以前没太注意过,所以查询资料的时候,看到网上对这个问题的解释几乎是相同(应该都是copy的),千篇一律且比较啰嗦...

Difference between Enumeration and Iterator in Java

深入比较Enumeration和Iterator的差别
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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