Objects in JavaScript
上一篇 /
下一篇 2012-03-07 15:34:49
/ 个人分类:JavaScript
1. 2 ways to create
javascript:;" onClick="javascript:tagshow(event, 'Object');" target="_self">
Object in JavaScript.:
Literal notationis the one where we declare the object, and, at the same time, define properties and values in the object. It makes use of{}
. See first one for an example.
Constructor notationis where we make use of the keywordsnewObject()
. We then use dot notation to add property and values to the object. See second one for an example.
//Literal var australia = { weather: "superb", people: "not many of them but they're all great!", tourism: "a wonderful place to holiday. please visit!"}; |
//Constructor var jordan = new Object(); jordan.weather = "hot. but so are the people!"; jordan.people = "see above!"; jordan.tourism = "Codecademy's dream team retreat!"; |
2. There are two different ways that we can retrieve an object's properties: dot and bracket notation.
- Dot notation: obj.myProp
- Bracket notation:obj["myProp"] (You can also use variable in bracket notation.
3. Functions in object are called Method.
4. this keyword: Why is using thethis
keyword helpful? Imagine we have a method that has a lot of complicated
code and references an object's properties. We also want many objects
to have this method. We can create a single generic method that uses thethis
keyword. Now many objects can use this method by defining a method that simply equals this generic method.
收藏
举报
TAG:
JavaScript
javascript
Object
object
对象