Skip to content

Commit bcf97b4

Browse files
sdraschrisvfritz
authored andcommitted
Update Instance in Guide to mention Object.freeze (#1323)
* update instance in guide to mention object.freeze * Small tweaks to Object.freeze example * Revert changes to spaces before parens
1 parent 242f0d0 commit bcf97b4

File tree

2 files changed

+2766
-7
lines changed

2 files changed

+2766
-7
lines changed

src/v2/guide/instance.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ order: 3
88

99
Every Vue application starts by creating a new **Vue instance** with the `Vue` function:
1010

11-
``` js
11+
```js
1212
var vm = new Vue({
1313
// options
1414
})
@@ -37,7 +37,7 @@ We'll talk about [the component system](components.html) in detail later. For no
3737

3838
When a Vue instance is created, it adds all the properties found in its `data` object to Vue's **reactivity system**. When the values of those properties change, the view will "react", updating to match the new values.
3939

40-
``` js
40+
```js
4141
// Our data object
4242
var data = { a: 1 }
4343

@@ -61,13 +61,13 @@ vm.a // => 3
6161

6262
When this data changes, the view will re-render. It should be noted that properties in `data` are only **reactive** if they existed when the instance was created. That means if you add a new property, like:
6363

64-
``` js
64+
```js
6565
vm.b = 'hi'
6666
```
6767

6868
Then changes to `b` will not trigger any view updates. If you know you'll need a property later, but it starts out empty or non-existent, you'll need to set some initial value. For example:
6969

70-
``` js
70+
```js
7171
data: {
7272
newTodoText: '',
7373
visitCount: 0,
@@ -77,9 +77,36 @@ data: {
7777
}
7878
```
7979

80+
The only exception to this being the use of `Object.freeze()`, which prevents existing properties from being changed, which also means the reactivity system can't _track_ changes.
81+
82+
```js
83+
var obj = {
84+
foo: 'bar'
85+
}
86+
87+
Object.freeze(obj)
88+
89+
new Vue({
90+
el: '#app',
91+
data () {
92+
return {
93+
obj
94+
}
95+
}
96+
})
97+
```
98+
99+
```html
100+
<div id="app">
101+
<p>{{ obj.foo }}</p>
102+
<!-- this will no longer update obj.foo! -->
103+
<button @click="obj.foo = 'baz'">Change it</button>
104+
</div>
105+
```
106+
80107
In addition to data properties, Vue instances expose a number of useful instance properties and methods. These are prefixed with `$` to differentiate them from user-defined properties. For example:
81108

82-
``` js
109+
```js
83110
var data = { a: 1 }
84111
var vm = new Vue({
85112
el: '#example',
@@ -103,7 +130,7 @@ Each Vue instance goes through a series of initialization steps when it's create
103130

104131
For example, the [`created`](../api/#created) hook can be used to run code after an instance is created:
105132

106-
``` js
133+
```js
107134
new Vue({
108135
data: {
109136
a: 1
@@ -120,7 +147,6 @@ There are also other hooks which will be called at different stages of the insta
120147

121148
<p class="tip">Don't use [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) on an options property or callback, such as `created: () => console.log(this.a)` or `vm.$watch('a', newValue => this.myMethod())`. Since arrow functions are bound to the parent context, `this` will not be the Vue instance as you'd expect, often resulting in errors such as `Uncaught TypeError: Cannot read property of undefined` or `Uncaught TypeError: this.myMethod is not a function`.</p>
122149

123-
124150
## Lifecycle Diagram
125151

126152
Below is a diagram for the instance lifecycle. You don't need to fully understand everything going on right now, but as you learn and build more, it will be a useful reference.

0 commit comments

Comments
 (0)