본문 바로가기
Happly Coding/CodeWars

[CodeWars] 8kyu#5 - Broken Greetings

by Hello Do. 2016. 5. 29.

[CodeWars] 8kyu#5 - Broken Greetings


■ Description:

Correct this code, so that the greet function returns the expected value.


■ Question:

1
2
3
4
5
6
7
function Person(name){
  this.name = name;
}
 
Person.prototype.greet = function(otherName){
  return "Hi " + otherName + ", my name is " + name;
}
cs


솔직히.. 뭘 어떻게 하라는건지.. 했다.
이번 문제를 풀기 위해서는 생성자 함수와 프로토타입이란 개념이 필요하다.
공부하자!!

Solution:

1
2
3
4
5
6
7
8
9
10
11
function Person(name){
  this.name = name;
}
 
Person.prototype.greet = function(otherName){
  return "Hi " + otherName + ", my name is " + this.name;
}
 
var Jake = new Person("Jake");
 
Jake.greet("Mason");
cs