Menu

Define Object Property in ES5

Object.defineProperty

It's very easy to define properties of an object in JavaScript.

var foo = {  
    bar: 1  
};

That's it. And it's very easy to change the property's value or type. foo.bar = "a string"; which is good for flexbility, and that's why we love JavaScript.

Now, what if we make bar a readonly member? Or even if we want more power to use set/get function on the object, which is pretty easy with C++, JAVA, C#.

An ES5 function Object.defineProperty() can kindof help. This function can define a new property on an object, or modifies an existing property on an object. Not like the very stright forward old style way, Object.defineProperty() supports more access controls on the property.

Object.defineProperty() has 3 parameters: the target object, the property name, and the descriptor. The descriptor itself is a simple object, has a few of properties, not only defines the property value, but also how the property could be accessed.

the descriptor

There are 2 types of properties at the descriptor object: 1. the value, 2. access right.

To define a property's value, we can use either a value property, or set/get properties of the descriptor.

value is

Welcome to Ghost

You're in! Nice. We've put together a little post to introduce you to the Ghost editor and get you started. Go ahead and edit this post to get going and learn how it all works!

Getting Started

Writing in markdown is really easy. In the left hand panel of Ghost, you simply write as you normally would. Where appropriate, you can use formatting shortcuts to style your content. For example, a list:

  • Item number one
  • Item number two
    • A nested item
  • A final item

or with numbers!

  1. Remember to buy some milk
  2. Drink the milk
  3. Tweet that I remembered to buy the milk, and drank it

Want to link to a source? No problem. If you paste in url, like http://ghost.org - it'll automatically be linked up. But if you want to customise your anchor text, you can do that too! Here's a link to the Ghost website. Neat.

What about Images?

Images work too! Already know the URL of the image you want to include in your article? Simply paste it in like this to make it show up:

The Ghost Logo

Not sure which image you want to use yet? That's ok too. Leave yourself a descriptive placeholder