markdown JS.Objects.classes.ES6getterand setter

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown JS.Objects.classes.ES6getterand setter相关的知识,希望对你有一定的参考价值。

// ES6 get and set
class Person {
    constructor(givenName, familyName) {
        this.givenName = givenName;    // "normal name"
			  this._familyName = familyName; // starts with "_"
    }
  
		// getters and setters are useful for processing
	  // properties, doing checks, changing them before
	  // returning their value, etc.
	  // having "get familyName" is equivalent to declaring a property
	  // named "familyName", but in this case we have to use ANOTHER
	  // name for the variable that will be used to store the property
	  // value. A convention is to keep the same name but add an 
	  // underscore at the beginning. 
    // Ex: get name(n) { this._name = n;}
	  
    get familyName() {
        return this._familyName.toUpperCase();
    }
  
    set familyName(newName) {
			  // validation could be checked here such as 
			  // only allowing non numerical values
        this._familyName = newName;   
    }
  
    walk() {
        return (this.givenName + ' ' + this._familyName + ' is walking.');
    }
}
         
let michel = new Person('Michel', 'Buffa');

document.body.innerHTML += "<p>" 
												+ michel.walk() 
												+ "</p>";

// Notice that we use here the "normal" names givenName and familyName
document.body.innerHTML += "<p>Our teacher is " 
												+ michel.givenName 
												+ ' ' + michel.familyName 
												+ "</p>";
JS.Objects.classes.ES6getterand setter
--------------------------------------


A [Pen](https://codepen.io/Onlyforbopi/pen/zJgNNR) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).

[License](https://codepen.io/Onlyforbopi/pen/zJgNNR/license).

以上是关于markdown JS.Objects.classes.ES6getterand setter的主要内容,如果未能解决你的问题,请参考以下文章

转换rst到markdown总结

markdown [Markdown HowTo]作为Markdown语法的秘籍

python markdown干啥用的

markdown前端渲染

如何用markdown生成目录

markdown排版示例