0

私はこのコードを持っています。displayメソッドを使用すると、次のようになります。

URL が未定義です

名前は未定です

説明は未定義です

すべての正当性を提供しているのに、なぜエラーが発生するのかわかりません。誰かが私のために問題を特定できますか?

function website(name,url,description)
{
    //Proparties
    this.name=name;
    this.url=url;
    this.description=description;

    // Methods
    this.getName=getName;
    this.getUrl=getUrl;
    this.getDescription=getDescription;
    this.display=display;

    // GetName Method
    function getName(name)
    {
        this.getName=name;
    }

    // GetUrl Method
    function getUrl(url){
        this.getUrl=url;
    }

    // getDescription
    function getDescription(description){
        this.getDescription=description;
    }

    function display(name,url,description){
        alert("URL is :" +url +" Name Is :"+name+" description is: "+description);
    }
}

// Set Object Proparites
web=new website("mywebsite","http://www.mywebsite.com","my nice website");

// Call Methods
var name = web.getName("mywebsite");
var url = web.getUrl("http://www.mywebsite.com");
var description = web.getDescription("my nice website");
web.display(name,url,description);
4

6 に答える 6

2

関数の仕組みについてかなり混乱していると思います。コードには次のものがあります。

this.getName=getName; // this sets a "getName" method on the "this" object
// to be some function that will be implemented below

function getName(name) // i believe this function shouldn't have any parameter...
{
this.getName=name; //now, you're overriding the "getName" that you set above,
// to be something completely different: the parameter you sent when calling this function!
// instead, you should do:
return name;
}
于 2013-04-25T10:00:23.507 に答える
1

私はそれを宣言する必要があります

function () {
  //property
  this.name

  //method
  this.setName = function ( name ) {
  this.name = name
  }
}

彼らはあなたがそれを実装する方法で、コンテキストの問題を求めます

于 2013-04-25T10:00:09.127 に答える
1

ゲッター関数は、自分自身を上書きするセッターです (?)。それらをに変更します

function getName(){
    return this.name;
}
function getUrl(){
    return this.url;
}
function getDescription(){
    return this.description;
}

function setName(name){
    this.name = name;
}
function setUrl(url){
    this.url = url;
}
function setDescription(description){
    this.description = description;
}

セッターが設定値を返すようにする場合はreturn、割り当ての前にキーワードを追加します。

于 2013-04-25T10:00:37.167 に答える
1

ゲッターは、ゲッター自体を再割り当てするのではなく、値を返す必要があります。

function getName() {
  return this.name;
}
于 2013-04-25T10:00:55.770 に答える
1

これ書きたかったの?:

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

私が理解しているように、プロパティを取得するのではなく、設定しています。そう:

var name = web.setName("mywebsite");
于 2013-04-25T09:57:19.863 に答える
0

各メソッドで次のように値を返す必要があります。

// GetName Method
function getName() {
    return this.getName = name;
}

// GetUrl Method
function getUrl() {
    return this.getUrl = url;
}

// GetDescription Method
function getDescription() {
    return this.getDescription = description;
}
于 2013-04-25T10:20:00.177 に答える