Skip to content

Commit

Permalink
Merge pull request loverajoel#422 from GeoffZhu/master
Browse files Browse the repository at this point in the history
fix error tip-61: Binding objects to functions
  • Loading branch information
zenopopovici authored May 31, 2017
2 parents eeb8175 + 295edc7 commit 0545ebb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
15 changes: 8 additions & 7 deletions _posts/en/javascript/2017-01-19-binding-objects-to-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post

title: Binding objects to functions
tip-number: 61
tip-username: loverajoel
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: Understanding how to use `Bind` method with objects and functions in JavaScript

Expand Down Expand Up @@ -42,19 +42,19 @@ A copy of the given function along with the specified `this` value and initial a
```js
const myCar = {
brand: 'Ford',
type: 'Sedan'
Color:Red
type: 'Sedan',
color: 'Red'
};

const getBrand = () => {
const getBrand = function () {
console.log(this.brand);
};

const getType = () => {
const getType = function () {
console.log(this.type);
};

const getColor = () => {
const getColor = function () {
console.log(this.color);
};

Expand All @@ -64,6 +64,7 @@ getBrand(myCar); // object not bind,undefined

getType.bind(myCar)(); // Sedan

getColor.bind(myCar); // Red
let boundGetColor = getColor.bind(myCar);
boundGetColor(); // Red

```
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post

title: 给函数 Bind 对象
tip-number: 61
tip-username: loverajoel
tip-username: loverajoel
tip-username-profile: https://github.com/loverajoel
tip-tldr: 理解在 JavaScript 中如何使用 `Bind` 方法绑定对象和函数

Expand Down Expand Up @@ -42,19 +42,19 @@ fun.bind(thisArg[, arg1[, arg2[, ...]]])
```js
const myCar = {
brand: 'Ford',
type: 'Sedan'
Color:Red
type: 'Sedan',
color: 'Red'
};

const getBrand = () => {
const getBrand = function () {
console.log(this.brand);
};

const getType = () => {
const getType = function () {
console.log(this.type);
};

const getColor = () => {
const getColor = function () {
console.log(this.color);
};

Expand All @@ -64,6 +64,7 @@ getBrand(myCar); // object not bind,undefined

getType.bind(myCar)(); // Sedan

getColor.bind(myCar); // Red
let boundGetColor = getColor.bind(myCar);
boundGetColor(); // Red

```
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ fun.bind(thisArg[, arg1[, arg2[, ...]]])
```js
const myCar = {
brand: 'Ford',
type: 'Sedan'
Color:Red
type: 'Sedan',
color: 'Red'
};

const getBrand = () => {
const getBrand = function () {
console.log(this.brand);
};

const getType = () => {
const getType = function () {
console.log(this.type);
};

const getColor = () => {
const getColor = function () {
console.log(this.color);
};

getBrand(); // 物件沒有被 bindundefined
getBrand(); // object not bind,undefined

getBrand(myCar); // 物件沒有被 bindundefined
getBrand(myCar); // object not bind,undefined

getType.bind(myCar)(); // Sedan

getColor.bind(myCar); // Red
let boundGetColor = getColor.bind(myCar);
boundGetColor(); // Red

```

0 comments on commit 0545ebb

Please sign in to comment.