How to change object property in JavaScript parameters
Example: I need to create a function as an object, if I have a simple object, I can get it like this:
myObject = function(){
alert('tada');
}
but I need to implement this function or other internal param object like this:
myObject = {
value : function(){
alert('tada!');
}
};
and call this function only myObject()
, not myObject.value()
, thanks
+3
source to share
4 answers
MyClass = function(){
return {
value : function(){
alert('tada!');
}
}
});
instance = MyClass();
instance.value();
or more appropriately
// Define a class like this
function MyClass(name){
this.name = name;
}
MyClass.prototype.sayHi = function(){
alert("hi " + this.name);
}
var instance = new MyClass("mike");
instance.sayHi();
0
source to share
myObject = (function() {
return {
value: function() {
alert('tada!');
}
`enter code here`
}
})();
myObject.value();
0
source to share