> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/creational-pattern-ok.md).

# Creational Pattern (ok)

http\://www\.giaosucan.com/2018/01/chem-gio-ve-javascript-design-pattern.html

Pattern này chuyên dùng để khởi tạo Object. Trong JavaScript có nhiều cách khác nhau để tạo object Ví dụ để tạo object Idol thì làm 1 trong 3 cách như sau

```
var idol = {}; // or
var idol = Object.create(null); // or
var idol = new Object();
```

Code trên tạo object Idol nhưng là object empty. Nhưng idol nào thì cũng phải có thuộc tính như chân, tay, mông, ngực Dưới đây là 3 cách để tạo thuộc tính cho idol. Chẳng hạn cần tạo thuộc tính chân cho idol, value là chân dài

```
// 1. Dot syntax
idol.leg = 'long leg'; // Write properties
var leg = idol.leg; // Access properties

// 2. Square bracket syntax
idol['leg'] = 'long leg'; // Write properties
var key = idol['leg']; // Access properties

// 3. Object.defineProperty
Object.defineProperty(idol, "leg", {
    value: "long leg",
    writable: true,
    enumerable: true,
    configurable: true
});
```
