Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fc70b6

Browse files
Merge pull request #89 from malipramod/question-35
Question 35 - forEach loop implementation
2 parents 6d78ae5 + cb6acce commit 8fc70b6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎README.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,16 @@ for (var i = 0; i < arr.length; i++) {
12801280
}
12811281
```
12821282
1283+
This can also achieve by forEach (allows you to keep that variable within the forEach’s scope)
12831284
1285+
```javascript
1286+
var arr = [10, 32, 65, 2];
1287+
arr.forEach(function(i) {
1288+
setTimeout(function() {
1289+
console.log('The index of this number is: ' + i);
1290+
}, 3000);
1291+
})
1292+
```
12841293
12851294
## Question 36. How to check if the value of a variable in an array?
12861295
@@ -3412,9 +3421,53 @@ console.log(mul(2)(3)(4)(5)(6));
34123421
34133422
Answer: 1) 720
34143423
3424+
### 7. What would be the output of following code ?
3425+
3426+
```javascript
3427+
function getName1(){
3428+
console.log(this.name);
3429+
}
3430+
3431+
Object.prototype.getName2 = () =>{
3432+
console.log(this.name)
3433+
}
3434+
3435+
let personObj = {
3436+
name:"Tony",
3437+
print:getName1
3438+
}
3439+
3440+
personObj.print();
3441+
personObj.getName2();
3442+
```
3443+
3444+
1. undefined undefined
3445+
2. Tony undefined
3446+
3. undefined Tony
3447+
4. Tony Tony
3448+
3449+
Answer: 2) Tony undefined
3450+
3451+
Explaination: **getName1()** function works fine because it's being called from ***personObj***, so it has access to *this.name* property. But when while calling **getnName2** which is defined under *Object.prototype* doesn't have any proprty named *this.name*. There should be *name* property under prototype. Following is the code:
34153452
3453+
```javascript
3454+
function getName1(){
3455+
console.log(this.name);
3456+
}
34163457

3458+
Object.prototype.getName2 = () =>{
3459+
console.log(Object.getPrototypeOf(this).name);
3460+
}
34173461

3462+
let personObj = {
3463+
name:"Tony",
3464+
print:getName1
3465+
}
3466+
3467+
personObj.print();
3468+
Object.prototype.name="Steve";
3469+
personObj.getName2();
3470+
```
34183471
34193472
34203473
## Contributing

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /