JavaScript:Prototype是静态的同义词吗? [英] JavaScript: Is Prototype Synonymous with Static?

查看:86
本文介绍了JavaScript:Prototype是静态的同义词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在教自己来自OOP背景的JavaScript。我正在研究的这本书让我想到,哇,属性几乎和静态方法或变量一样!如果是这种情况我会更喜欢实施它们以节省一些记忆,但在我爱上它们之前,我想确保我正确地使用它们。

I am teaching myself JavaScript coming from a OOP background. The book in which I am studying has made me think, "Wow, properties are almost the same as static methods or variables!" If this is the case I would love to implement them more to save some memory, but before I fall in love with them I want to make sure I'm using them correctly.

这是这种情况吗?我的逻辑错了吗?我在下面添加一些示例代码作为我的问题的上下文。希望它不会过于简单:

Is this the case? Is my logic wrong? I am adding some sample code below to use as context for my question. Hopefully it's not oversimplified:

function person(first, age){
    this.firstName = first;
    this.age = age;
}

person.prototype.sayHello = function(){
    return "hi my name is " + this.firstName + " and I am " + age + ".";
};

因此构造函数可以通过以下方式调用

and thus the constructor function could be called the following way

var me = new person("Dan", 22);

此外,这会破坏封装吗?上面的示例没有声明类中的变量,因此它们将是全局范围的。我知道如果声明 var,原型将无法看到 firstName age firstName var age

Also, does this break encapsulation? The example above does not declare the variables in the class so they will be globally scoped. I understand the prototype would not be able to see firstName or age if they were declared var firstName and var age.

我必须选择其中一个吗?我可以不使用我添加的原型和封装吗?

Do I have to pick one or the other? Can I not use my added prototypes and encapsulation?

提前致谢!

推荐答案


原型是否与静态同义?

Is Prototype Synonymous with Static?

没有。原型上的属性是实例属性。如果将方法附加到函数的原型,则从该函数创建的实例将从该原型继承。这意味着从该函数创建的所有实例将在原型上共享该方法的单个副本。

No. Properties on the prototype are instance properties. If you attach a method to the prototype of a function then instances created from that function will inherit from that prototype. This means that all instances created from that function will share a single copy of that method on the prototype.

JavaScript中的静态属性最好由属性直接表示在构造函数本身上:

A 'static' property in JavaScript is best represented by a property directly on the constructor function itself:

function Person() {}
Person.count = 0;

此处, count 是'静态'构造函数上的属性。你可以从例如更新在构造函数中使用 Person.count ++ ;

Here, count is a 'static' property on the constructor. You could update that from e.g. within the constructor with Person.count++;


上面的示例未声明类中的变量因此它们将是全局范围的

The example above does not declare the variables in the class so they will be globally scoped

您没有任何变量声明,只有属性赋值。因为原型方法将在实例的上下文中调用,所以将引用该实例,因此原型方法可以访问这些实例属性。

You don't have any variable declarations, only property assignments. Because the prototype method will be called in the context of an instance this will refer to that instance and therefore the prototype method has access to those instance properties.

你的例子中唯一的'全局'是 person 构造函数本身。

The only thing that is 'global' in your example is the person constructor itself.

这篇关于JavaScript:Prototype是静态的同义词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆