简体   繁体   English

我不知道的JavaScript难题

[英]JavaScript Puzzle I can't figure out

This is a kata for code wars, and I can't seem to figure it out. 这是代码大战的手段,我似乎无法弄清楚。 I have never worked with JavaScript before. 我以前从未使用过JavaScript。

I know the answer is probably simple, but I just can't seem to figure out what they are looking for even after many hours of searching. 我知道答案可能很简单,但是即使经过数小时的搜索,我似乎也无法弄清楚他们正在寻找什么。 I know that name in the greet function is not defined, but when I define it, it says it's not the value it's looking for. 我知道greet函数中的name未定义,但是当我定义它时,它表示它不是要查找的值。

function Person(name){
  this.name = name;
}

Person.prototype.greet = function(otherName){
  return "Hi " + otherName + ", my name is " + name;
}

Please help and an explanation would be greatly appreciated. 请帮忙,一个解释将不胜感激。

Don't really understand what you are looking for but hope this will shed some light : (try on your console) 不太了解您要寻找的内容,但希望它能对您有所帮助:(在控制台上尝试)

function Person(name){
  this.name = name;
}

Person.prototype.greet = function(otherName){
  return "Hi " + otherName + ", my name is " + this.name;
}

var p = new Person('jack');

p.greet('sparrow');

Tyagi gave you an explanation of how to call it but did not show what the actual problem with the code is: Tyagi给了您有关如何调用它的解释,但没有显示代码的实际问题是:

Here's my (very similar) example: 这是我的例子(非常相似):

function Person(name) {
  this.name = name;
}

Person.prototype.greet = function (otherName) {
  return "Hi " + otherName + ", my name is " + this.name;
}

var john = new Person("John");

$("#result").text(john.greet("Mike"));

and if you click through to this JSFiddle then you can see it actually working. 如果您单击该JSFiddle,则可以看到它确实有效。 The difference between the two is simply the change of "name" to "this.name" in the greet() function. 两者之间的区别只是在greet()函数中将“名称”更改为“ this.name”。 You're attaching a new function to every Person object but it doesn't automatically look for the name variable on the object within that function the way it's defined. 您正在向每个Person对象附加一个新函数,但它不会按照定义的方式自动在该函数内的对象上查找name变量。

I don't get your question but i will try to explain how it works: 我没有收到您的问题,但我将尝试解释其工作原理:

// define a function (or class) called 'Person'
function Person(name){
  // add data members 'name' to 'this' pointer which points to current context
    this.name = name;
}

// define a method in 'Person' class 
Person.prototype.greet = function(otherName){
    //'othername' is an argument which you passed while calling 'greet' method
    //'name' is an data memeber which you declared while creating 'Person' class
    // will return value when you call this method
    return "Hi " + otherName + ", my name is " + this.name;
}

// create a class object
var person = new Person('Mohit');
// call its 'greet' method
// will show an alert 'Hi Bekk, my name is Mohit'
alert(person.greet('Bekk'));

JSFiddle Link: http://jsfiddle.net/QqnL5/ JSFiddle链接:http://jsfiddle.net/QqnL5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM