I need a function to work for only the given object. I’m not sure if its possible, but I tried something like:
var a = {
b: function(a) {
return display(a)
}
}
a.prototype.display = function(a) {
return a;
}
alert(a.b('Hi'))//This is suppose to work
alert(display(a))//This isn't suppose to work
This doesn’t work though, not sure why. I’m kinda new to prototype. I used it with String.prototype for example but all the other stuff I still need to learn. Thanks for the help.
You need a private method in your object. The only way to achieve this in javascript is to keep the function in closure and execute it in the context of current object.
Here
displayis not accessible outside.a.display("hi");is not accessible.