methods as properties of an object in javascript -


I need to create a javascript function with a personal variable that has setter and geter methods. I tried:

  create a functional secreter (secret) {this._secret = secret; Var getSecret = function () {return._secret; } Var setSecret = function (secret) {this._secret = secret; }}  

and a variant with:

  this.getSecret = function () ...  

And

  this.seSecret = function () ...  

This code is not going to be a test suit on wars like something

  var obj = createSecretHolder (secret); Obj.getSecret (); Obj.setSecret (newSecret);  

and others who are hidden I get an error TypeError: Undefined property can not be read 'getSecret' and no other method can be set SEecret

builder does not support return a value Then createSecretHolder (Incognito) returns undefined .

then var obj = createSecretHolder (secret); To define obj so when you obj.getSecret () .

If you try to access the error "Property can not be read, getSecret by using var a function The variable function described is scotched. So when you try to access it outside the function, as you do in obj.getSecret () , it will not work.

You also do not understand how this works will not create a personal variable .

There are several ways to do this: Here is one:

  Create a function secthilder (mySecret) {var secret = mySecret; return {getSecret: function () {return secret ;}, SetSecret: function (mySecret) {secret = mySecret;}};}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -