javascript - Why is the title not being set properly after the instance variable has been created? -


i trying setup basic example can expand upon main program. question here why title not equal "heybark"? made book part of booktitles class shouldn't title being set automatically add bark?

after done need reference capitalization method created in original assignment. idea once book title set in snippet below, pass value method , set equal return result. how access title modified since setter method take in entire object?

class booktitle {   constructor(title){   this.title = title + "bark"; }  // in comments part of second paragraph in  // question, psuedo code  // getter object here access title  // use setter call capitalization method capitalize  // words in title  // title creator method returns modified value, called in setter  // cap method purely caps correct words, called within  // title creator }  var book = new booktitle(); console.log(book); //title undefinedbark expected book.title = "hey"; console.log(book); // returns hey, not heybark 

if helps here actual code current working solution, nice dev able make work in isolated environment here other day trying modify work in setter way.

titlecreator(string) {         // note isn't meant fledged title creator, designed pass these specific tests         var littlewords = ["and", "over", "the"]; // these words don't want capitalize          var self = this; // doesn't need here, syntax sugar, using searches things inside class          var modifiedstring = string         .split(' ') // splits string array of words, breaks sentence         .map(function(word,index) {             if (index == 0) {                 return self.capitalize(word); // capitalize first word of string             } else if (littlewords.indexof(word) == -1) {                 return self.capitalize(word); // capitalize words not little, -1 returned indexof if can't find word in array             } else if (littlewords.indexof(word) >= 0) {                 return word; // not capitalize word in list of littlewords             }         })         .join(' '); // joins every element of array string space inbetween each value. created sentence array of words          return modifiedstring;      }      capitalize(word) {         return word.charat(0).touppercase() + word.slice(1);         // function capitalizes word given     } 

i think, misunderstood constructor concept here. constructor builds new instance of given class, in case create instance of booktitle class. after new instance created, constructor doesn't have power on it, anymore. it's practice name classes capital letters , instances lowercase.

to achieve, want, need specify getter , setter property, example:

class booktitle {    constructor(title){      this.title = title;    }        title() {      return this._title ;    }        set title(title) {      this._title = title + 'bark';    }  }    let booktitle = new booktitle();  booktitle.title = 'test';  console.log(booktitle.title);


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -