[removed] How to add getter to an existing object

前端 未结 5 1470
说谎
说谎 2020-12-09 14:35

I can have a getter in a JavaScript object like this:

var member = {
    firstName:"XYZ", 
    lastName:"zzz", 
    get fullName(){ return         


        
5条回答
  •  天涯浪人
    2020-12-09 15:11

    You can only use get and set in a Class, It's mean that getter and setter method. but you can add a function to class:

    member.isGuest = function isGuest(){ return this.firstName=='Guest';}
    
    member.isGuest()
    

    the get means that property can be read! the set means that property can be write! You can look it in book 《JavaScript:The.Definitive.Guide》6edition the 6 chapter!

提交回复
热议问题