Javascript静态vs实例,原型关键字 [英] Javascript static vs instance, prototype keyword
问题描述
我想知道以下两个代码片段之间的区别
I want to know the difference between the following two code snippets
我的理解是这是静态的,因为没有使用new关键字,getCookie和setCookie函数创建实例可以被调用。
What I understand is this is static because without creating an instance with new keyword, getCookie and setCookie functions can be called.
var CookieHandler = function () {};
CookieHandler.getCookie = function (key) {
};
CookieHandler.setCookie = function (key, value) {
};
这就是实例。在这种情况下,您需要创建一个实例来调用函数。
And this is instance. In this case you need to create an instance to call the functions.
var CookieHandler = function () {};
CookieHandler.prototype.getCookie = function (key) {
};
CookieHandler.prototype.setCookie = function (key, value) {
};
我是一名java程序员,几乎不了解JS概念,请帮助我。
I was a java programmer and barely understanding JS concepts please help me with this.
推荐答案
在Javascript中,任何函数也都是一个对象,因此任何函数都可以赋值给它。这就是你的第一个代码块正在做的事情。它只是为 CookieHandler
函数分配属性。如果CookieHandler是一个对象定义,那么虽然不完全相同,但它们类似于其他OO语言中的类静态属性。
In Javascript, any function is also an object, so any function can have properties assigned to it. That's what your first block of code is doing. It is just assigning properties to the CookieHandler
function. If CookieHandler is meant as an object definition, then though not exactly identical, these are analogous to class static properties in other OO languages.
如果它不是一个对象定义,那么CookieHandler就像一个Javascript命名空间, getCookie
和 setCookie
就像该命名空间中的属性一样。
If it is not meant as an object definition, then CookieHandler is like a Javascript namespace and getCookie
and setCookie
are like properties in that namespace.
您的第二个代码块是为原型分配属性。这些属性将由实例化的CookieHandler对象继承。
Your second block of code is assigning properties to the prototype. These properties will be inherited by an instantiated CookieHandler object.
因此,使用您的第一个代码块:
So, with your first block of code:
var CookieHandler = function () {};
CookieHandler.getCookie = function (key) {
};
CookieHandler.setCookie = function (key, value) {
};
您可以随意拨打 CookieHandler.getCookie()
随时。 CookieHandler
类似于命名空间对象, getCookie
和 setCookie
是名称空间上的属性。
You can just freely call CookieHandler.getCookie()
at any time. CookieHandler
is like a namespace object and getCookie
and setCookie
are properties on the namespace.
如果您创建CookieHandler对象,例如:
If you create a CookieHandler object such as:
var x = new CookieHandler();
x.getCookie(); // does not work
x.setCookie(); // does not work
然后, x
不会有 getCookie()
或 setCookie()
方法。这些方法仅存在于 CookieHandler
对象中。它们不会被CookieHandler实例继承。
Then, x
would not have getCookie()
or setCookie()
methods. Those methods only exist on the CookieHandler
object. They are not inherited by instances of CookieHandler.
使用第二个代码块:
var CookieHandler = function () {};
CookieHandler.prototype.getCookie = function (key) {
};
CookieHandler.prototype.setCookie = function (key, value) {
};
您正在定义 CookieHandler
实例的属性将继承(实例继承原型上的属性)。所以,当你这样做时:
you are defining properties that instances of CookieHandler
will inherit (instances inherit properties on the prototype). So, when you do:
var x = new CookieHandler();
x.getCookie(); // works
x.setCookie(); // works
所以,使用<$ c当你想要定义对象实例将继承的属性(通常是方法)时,$ c> prototype 。如果您没有尝试定义实例方法,那么只需将方法放在第一个代码块中的任何对象上。
So, use the prototype
when you want to define properties (usually methods) that instances of the object will inherit. If you aren't trying to define instance methods, then just put the methods on any object as in your first block of code.
这篇关于Javascript静态vs实例,原型关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!