JavaScript Can't Access Private Properties

Typescript private property subclass can't access

You need to change private to protected.
from typescriptlang.org:

TypeScript’s private #
TypeScript also has it’s own way to declare a member as being marked private, it cannot be accessed from outside of its containing class.

-- means private fields are going to be in all subclasses but you won't be able to access it.

Understanding protected #
The protected modifier acts much like the private modifier with the exception that members declared protected can also be accessed within deriving classes.

-- it's what you need, the fields which can't be access from outside but available in all subclasses.

Can you extend an object that has access to private properties with a function that can also access those private properties?

I hate to answer my own question - seems like a bit of a faux pas - but c'est la vie. (because I woke up French today?)

So, while I found that the eval() solution I presented last night in the edit to my original question does seem to be a valid solution, and a proper use of eval for retaining the object's context within the new function, it is far from perfect.

Firstly, it works in FF, but both IE and Chrome seem to hate it (those were the next ones I tried, and I quit trying others after they both failed). Though I'm sure it could probably be made to work across browsers, it seems like a hassle.

Secondly, it does give quite a bit of power to the new function, and as I look at my code more I do like the idea of controlling exactly what these new functions being added to my object get access to.

Thirdly, .eval() is typically pretty slow - and it turns out that .apply() (which is typically faster) just may work well enough.

This is because I realized at some point last night that no new functions on this object will need to set any of the private variables (at least, I'm fairly certain they won't) - and .apply() works fine to pass the values through for them to read.

I'm sure there's more to it than just those 3 things, but for now I think I'm going to go with more of a 'wrapper' solution - something like this:

var f = function (){
var fauxThis = {};

fauxThis.priv = priv;

obj1.fn2.apply(fauxThis, arguments);
};

obj3.fn2 = f;

//(To be placed where I had "obj3.fn2 = obj1.fn2;")

I am certainly willing now to consider the use of eval() in very specific cases - and may even revisit this specific use of it before I make my final decision of which direction to take. (especially if I can think of a case where the private value would need to be set)

Thanks all for your input!

Accessing private variables in javascript

Your private variable is local to that function scope and within Javascript, there is no access to variables inside a scope from outside the scope. You can ONLY access it from within the scope. There are no ways to get around this from Javascript code itself.

What a debugger can do (which has access to the VM internals) is different than what regular Javascript code can do. The debugger can look inside of scopes, but JS code from outside the scope cannot.

Obviously you can make an accessor for it, but without an accessor, there is no way to get to it from the outside.

Can't access object property even though should be there

Issue is at this line -

if (response.status !== "200") {
}

It should be 200 instead of "200". You can check the console.log("response is", response);, it's numeric 200 not string.

Note: Always use the below construct to setState for objects -

{ ...previousState, { newkey: newvalue } }

The above uses the previous values of object and replaces with new values.

Private properties in JavaScript ES6 classes

Private class features is now supported by the majority of browsers.

class Something {
#property;

constructor(){
this.#property = "test";
}

#privateMethod() {
return 'hello world';
}

getPrivateMessage() {
return this.#property;
}
}

const instance = new Something();
console.log(instance.property); //=> undefined
console.log(instance.privateMethod); //=> undefined
console.log(instance.getPrivateMessage()); //=> test
console.log(instance.#property); //=> Syntax error

Unable To Access Private Properties In JavaScript Facade Pattern

This is because this does not correspond to what you think it does.

You pass the resizeThrottler method as a reference for the browser to call, but when it does call it, the context is no longer your object, but window, which evidently does not have the same properties.

You could solve this as follows:

 window.addEventListener('resize', _.resizeThrottler.bind(_), false);

This way you set the context of the method passed as handler to always run with _ as this.



Related Topics



Leave a reply



Submit