Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between attribute and property? [closed]

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!


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 :).


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".


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.


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.