属性和属性有什么区别?

发布于 2024-07-08 11:07:44 字数 1450 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

此生挚爱伱 2024-07-15 11:07:45

一般来说(以及正常的英语用法)这些术语的含义相同。

在 HTML / Javascript 的特定上下文中,这些术语会变得混乱,因为 DOM 元素的 HTML 表示具有属性(该术语在 XML 中用于标记中包含的键/值对),但是当表示为 JavaScript 对象,这些属性显示为对象属性

更令人困惑的是,对属性的更改通常会更新属性。

例如,更改 element.href property 将更新元素上的 href attribute,这将反映在对 element.getAttribute('href') 的调用中。

但是,如果您随后读取该属性,它将被标准化为绝对 URL,即使属性可能是相对 URL!

In general terms (and in normal English usage) the terms mean the same thing.

In the specific context of HTML / Javascript the terms get confused because the HTML representation of a DOM element has attributes (that being the term used in XML for the key/value pairs contained within a tag) but when represented as a JavaScript object those attributes appear as object properties.

To further confuse things, changes to the properties will typically update the attributes.

For example, changing the element.href property will update the href attribute on the element, and that'll be reflected in a call to element.getAttribute('href').

However if you subsequently read that property, it will have been normalised to an absolute URL, even though the attribute might be a relative URL!

梦罢 2024-07-15 11:07:45

这些词早在计算机科学出现之前就已经存在了。

  1. 属性是我们归因于某人或某物的品质或对象。 例如,权杖是权力和国家地位的属性。

  2. 财产是一种无需任何归属而存在的品质。 例如,粘土具有粘合性; 即,粘土的一个特性是它的粘合性。 又如:金属的特性之一是导电性。 属性通过物理现象来展示自己,而不需要将它们归因于某人或某物。 同样的道理,说某人具有男性特征也是不言而喻的。 实际上,您可以说财产归某人或某物所有。

公平地说,在计算机科学中,这两个词至少在大多数情况下可以互换使用 - 但程序员通常没有英语文学学位,也不会编写或不太关心语法书:)。

These words existed way before Computer Science came around.

  1. Attribute is a quality or object that we attribute to someone or something. For example, the scepter is an attribute of power and statehood.

  2. Property is a quality that exists without any attribution. For example, clay has adhesive qualities; i.e, a property of clay is its adhesive quality. Another example: one of the properties of metals is electrical conductivity. Properties demonstrate themselves through physical phenomena without the need to attribute them to someone or something. By the same token, saying that someone has masculine attributes is self-evident. In effect, you could say that a property is owned by someone or something.

To be fair though, in Computer Science these two words, at least for the most part, can be used interchangeably - but then again programmers usually don't hold degrees in English Literature and do not write or care much about grammar books :).

海的爱人是光 2024-07-15 11:07:45

属性是您在 HTML 标记中使用的实际内容,例如

<input type="checkbox" checked="checked" />

在此实例中类型和检查的属性。 但属性是这些属性的值,浏览器将其保存在 DOM 元素内。 通常,属性和特性的值是相等的,这就是它如此令人困惑的原因。

在此示例中,DOM 元素 input 的属性 type 的值为 "checkbox" ,属性 checked 的值为值 true (请注意该值与 HTML 属性内的值有何不同)。

使用 Firebug,您可以在单击元素并选择“DOM 视图”时观察属性的行为。

An attribute is the actual thing that you use within your HTML tag like

<input type="checkbox" checked="checked" />

In this instance type and checked are attributes. The property though is the value of these attributes, which the browser saves inside the DOM element. Often the value of the attributes and the properties are equal, that's what makes it so confusing.

In this example the DOM element input has the property type with the value "checkbox" and the property checked with the value true (notice how this value differs from the value inside the HTML attribute).

Using Firebug you can observe the behaviour of properties when clicking on an element and selecting the "DOM view".

萌逼全场 2024-07-15 11:07:45

通常,属性用于描述机制或现实世界的事物。

属性用于描述模型。

例如,一份文档(放在您的桌子上)可能具有它是草稿的属性。

建模文档的类有一个属性来指示它是否是草稿。 在这种情况下,属性捕获状态。

Often an attribute is used to describe the mechanism or real-world thing.

A property is used to describe the model.

For instance, a document (sitting on your desk) may have the attribute that it is a draft.

The class that models documents has a property to indicate whether or not it's a draft. In this case the property captures the state.

追我者格杀勿论 2024-07-15 11:07:45

这些术语的确切含义在很大程度上取决于您所谈论的语言/系统/宇宙。

例如,在 HTML/XML 中,属性是带有等号和值的标记的一部分,而属性并不意味着任何内容。

因此,我们需要有关您正在讨论的领域的更多信息。

The precise meaning of these terms is going to depend a lot on what language/system/universe you are talking about.

In HTML/XML, an attribute is the part of a tag with an equals sign and a value, and property doesn't mean anything, for example.

So we need more information about what domain you're discussing.

笔芯 2024-07-15 11:07:45

在 Python 中...

class X( object ):
    def __init__( self ):
        self.attribute
    def getAttr( self ):
        return self.attribute
    def setAttr( self, value ):
        self.attribute= value
    property_name= property( getAttr, setAttr )

属性是一个类似于属性的名称,它包装了 setter、getter(和删除器)函数的集合。

属性通常是另一个对象中的单个对象。

然而,话虽如此,Python 为您提供了诸如 __getattr__ 之类的方法,这些方法允许您扩展“属性”的定义。

底线 - 它们几乎是同义词。 Python 在它们的实现方式上做出了技术上的区别。

In Python...

class X( object ):
    def __init__( self ):
        self.attribute
    def getAttr( self ):
        return self.attribute
    def setAttr( self, value ):
        self.attribute= value
    property_name= property( getAttr, setAttr )

A property is a single attribute-like name that wraps a collection of setter, getter (and deleter) functions.

An attribute is usually a single object within another object.

Having said that, however, Python gives you methods like __getattr__ which allow you extend the definition of "attribute".

Bottom Line - they're almost synonymous. Python makes a technical distinction in how they're implemented.

肥爪爪 2024-07-15 11:07:45

属性和属性有什么区别?
特征和功能有什么区别?
特征 和 性格 和有什么不一样?
行为和行为有什么区别?

这只是上下文的变化。

对象、产品、个性、人

一个人的行为。
人格具有给定角色的特征。
产品具有派生功能的特征。
对象具有赋予其属性的属性。

What is the difference between Attribute and Property?
What is the difference between Feature and Function?
What is the difference between Characteristic and Character?
What is the difference between Act and Behavior?

Its just a change in context.

Object,Product,Personality,Person

A Person Acts in a Behavior.
A Personality has Characteristics of a given Character.
A Product has Feature that derive Functionality.
An Object had Attributes that give it Properties.

朮生 2024-07-15 11:07:45
<property attribute="attributeValue">proopertyValue</property>

将是看待它的一种方式。

在 C# 中

[Attribute]
public class Entity
{
    private int Property{get; set;};
<property attribute="attributeValue">proopertyValue</property>

would be one way to look at it.

In C#

[Attribute]
public class Entity
{
    private int Property{get; set;};
时光礼记 2024-07-15 11:07:45

在 Java(或其他语言)中,使用 Property/Attribute 取决于用法:

  • 当值不经常更改时使用的属性(通常在启动时或用于环境变量)

  • 属性是元素(对象)的值(对象子级),它可以经常/一直更改并且可以持久化或不持久

In Java (or other languages), using Property/Attribute depends on usage:

  • Property used when value doesn't change very often (usually used at startup or for environment variable)

  • Attributes is a value (object child) of an Element (object) which can change very often/all the time and be or not persistent

暮年 2024-07-15 11:07:45

在 HTML 中,属性似乎是 DOM 树特有的,而属性则用于描述 DOM 元素的特征

In HTML it seems attributes are specific to the DOM tree while properties are used to describe the characteristics of DOM elements

城歌 2024-07-15 11:07:45

Delphi 使用了属性,并且它们已经进入 .NET(因为它具有相同的架构师)。

在 Delphi 中,它们通常与运行时类型信息结合使用,以便可以使用集成属性编辑器在设计时设置属性。

属性并不总是与字段相关。 它们可能是有副作用的函数(当然这是非常糟糕的设计)。

Delphi used properties and they have found their way into .NET (because it has the same architect).

In Delphi they are often used in combination with runtime type information such that the integrated property editor can be used to set the property in designtime.

Properties are not always related to fields. They can be functions that possible have side effects (but of course that is very bad design).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文