为什么访问map方法时传入的值未定义?

时间:2017-09-12 00:10:39

标签: javascript

当我在JSbin中运行下面的代码片段时,它说大写字母不会起作用,因为该属性未定义。我查看了文档,并提到使用“'这个”这个词。但我不太确定这是否适用于此,因为传入的字符串值是正确的(控制台记录它以确认)。为什么大写方法不是苹果可以解决地图值?

class bookTitle {   
    constructor(title) {
        this.title = title; // this creates the object ONCE, see top of jasmine file for explanation
    }

    get title() {
        return this._title; // retrieves the title string
    }

    set title(title) {
        this._title = this.titleCreator(title); // calls the method and sets title, see top of jasmine file for explanation
    }

    titleCreator(string) {

      if (string == null){
        return string; // catches first error
      }


        // Note that this isn't meant to be a fully fledged title creator, just designed to pass these specific tests
        var littleWords = ['and', 'do', 'the', 'a', 'an', 'in', 'of']; // These are the words that we don't want to capitalize

        var modifiedString = string
        .split(' ') // Splits string into array of words, basically breaks up the sentence
        .map(function(word,index) {
            if (index == 0) {
                return this.capitalize(word); // capitalize the first word of the string
            } else if (littleWords.indexOf(word) == -1) {
                return this.capitalize(word); // capitalize any words that are not little, the -1 is returned by indexOf if it can't find the word in the array
            } else if (littleWords.indexOf(word) >= 0) {
                return word; // do not capitalize as this word is in the list of littleWords
            }
        })
        .join(' '); // Joins every element of an array into a string with a space inbetween each value. Basically you created a sentence from an array of words

        return modifiedString;

    }

    capitalize(word) {
        return word.charAt(0).toUpperCase() + word.slice(1);
        // This function just capitalizes the word given to it
    }
}

let bookTitles = new bookTitle();
bookTitles.title = 'inferno';
console.log(bookTitles.title); // The goal is to output Inferno

1 个答案:

答案 0 :(得分:1)

问题在于Invoke-RestMethod -Uri http://api.newrelic.com/v2/applications/17303495/deployments.json -Method 'POST' -Headers System.Collections.Hashtable -ContentType 'application/json' -Body { "deployment": { "revision": "Deployment of e0ca4b7", "changelog": "See Release Email Notes", "description": "Beginning Deployment of e0ca4b7", "user": "pcort" } } 中的this是指您传递给map的功能。请改为使用箭头函数map(word, index) => { ... }应该转到父类。

this