Creating a class with private properties

I am working on a utility for creating classes in javascript. And it works, the problem is how to define private attributes.

This is the code

var OO = { Class:function(){ var len = arguments.length; var data = arguments[len-1]; var Klass; if (data.constructor === Object){ Klass = function (){}; } else { Klass = data.constructor; delete data.constructor; } OO.extend(Klass.prototype,data); //Classic Extend Method return Klass; }, //Simple extend method, just what I need in this case extend: function(target, source){ var prop; for (prop in source) target[prop] = source [prop]; } } 

Here is how it works

 // first create a class var person = OO.Class ({ constructor: function (name, age) { this.name = name; this.age = age; }, name:'', age:'', getName: function () { return this.name; }, getAge: function () { return this.age; } }); 

And here is an instance

 var one = new Person ('josh', 22); 

And the problem is:

 one.age / / returns 22 one.name / / returns josh 

I need only methods like getName () and getAge () access these properties

EDIT1 : added extension function

+6
source share
4 answers

Update 1:

Here it is a working prototype. It works except

1.) Transfer of construction values โ€‹โ€‹to private members.

  Dummy values do work however. 

Check out the script:

http://jsfiddle.net/UWRHP/11/

0
source

The closure is created by the constructor parameters, so thatโ€™s all you need to do (AlienWebguy edited code):

 var Person = function(name, age){ this.getName = function() { return name; }; this.getAge = function() { return age; }; }; var john = new Person('johnathan', 33); document.writeln(john.name); // undefined document.writeln(john.age); // undefined document.writeln(john.getName()); // johnathan document.writeln(john.getAge()); // 33 
+1
source

Any variable that is part of the object's interface (i.e. this.x and this.y ) will be publicly available. You cannot force external access to these variables to go through getters.

So, as long as you have this code:

 getAge: function () { return this.age; } 

... you cannot prevent this:

 var one = new Person ('josh', 22); console.log(one.age); 

Check out Douglas Crockford's article Private Members in JavaScript .

-1
source

If you want to not complicate the situation (and completely abandon what you have done so far), as @AlienWebguy suggests, you donโ€™t even need the .io element, which, in the end, displays the same way as the .name properties and .age :

 john.io.set('name', 'Johnny'); // still exposed! 

In the end, the .io object is as open as the .name and .age , so it is a complex solution that does not make any progress in encapsulating or hiding information. (sorry @AlienWebguy, this is how I feel)

If you are trying to use the classic inheritance paradigm and completely abandon the usual OOP JavaScript, then abandon the traditional paradigm of function-as-constructors and forget about using the this link in your constructors:

 // first create a constructor var Person = function(arg0, arg1) { var that = {}; // private variables var age = arg0; var name = arg1; // public methods that.getName = function() { return name; }; that.getAge = function() { return age; }; that.setAge = function(a) { age = a; }; return that; }; var john = new Person('johnathan', 33); console.log(john.name); // undefined console.log(john.getName()); // 'johnathan' console.log(john.age); // undefined console.log(john.getAge()); // 33 john.setAge(28); console.log(john.getAge()); // 28 john.setName("Richard"); // Error - setName is not a function 

This is an example of Douglas Crockford Parasitic inheritance ... but without part of the "inheritance". The fact is that the instance variables age and name are private, but remain in the visible range due to the visibility of the JavaScript functional variable, so object methods can still control them.

You will also notice that the name variable does not have a setter, so this method will legally allow you to control access to the name variable - there are no hidden this.name and this.io that will allow the variable to be manipulated; it is completely closed.

-1
source

Source: https://habr.com/ru/post/924804/


All Articles