I have two objects of the same type.
function myObject(){
this.a = 1;
this.b = 1;
function changeA(){//some code};
function changeB(){//some code};
}
var obj1 = new myObject();
var obj2 = new myObject();
How can I make a call to obj2.changeB() from external code, another function or another object (e.g. obj1) ?
obj2.changeB() doesn't exist.
You need to assign a property on your object, not create a local variable:
this.changeB = function() { ... };
Just create a properties in you object like:
function myObject(){
this.a = 1;
this.b = 1;
this.functionA = function changeA(){//some code
alert('im 1');
};
this.functionb = function changeB(){//some code
alert('im 2');};
}
and call the function obj2.functionb();
LIVE DEMO
You have to do something like that:
var myObject = function(){
var protectedValue1 = ‘variable’;
var protectedValue2 = ‘variable’;
var changeA = function(){
alert(protectedValue);
}
var changeB = function(){
alert(protectedValue);
}
}
var object1 = new myObject();
var object2 = new myObject();
//
object2.changeB();
Related
I created an object with Int values and some functions in js as below
var obj = function (int1,int2,int3){
this.int1=int1;
this.int2=int2;
this.int3=int3;
this.add= (function (){
return parseInt(int1)+parseInt(int2)+parseInt(int3)
}());
When I execute the code
var a= new obj(1,2,3);
console.log(a.add);
The Answer is
NaN
Type of all the int1,int2.int3,add is Number.
Whats the problem with My code & How to correct it
You need a instance of the function with new operator and you could skip parseInt, which works better with a given base.
var Obj = function (int1, int2, int3) {
this.int1 = int1;
this.int2 = int2;
this.int3 = int3;
this.add = function () {
return this.int1 + this.int2 + this.int3;
}
},
instance = new Obj(1, 2, 3);
console.log(instance.add());
Try this:
var obj = function(int1, int2, int3) {
this.int1 = int1;
this.int2 = int2;
this.int3 = int3;
this.add = function() {
return parseInt(int1) + parseInt(int2) + parseInt(int3)
}};
And, Call above as:
var a = new obj(1, 2, 3);
console.log(a.add());
run this code:
var obj = function (int1, int2, int3) {
this.int1 = int1;
this.int2 = int2;
this.int3 = int3;
this.add = (function () {
return parseInt(int1) + parseInt(int2) + parseInt(int3)
} ())
};
var a = new obj(1,2,3);
console.log(a.add); // 6
your code lost a }
Try this it's working fine :
var obj = function (int1,int2,int3){
this.int1=int1;
this.int2=int2;
this.int3=int3;
this.add= (function (){
return parseInt(int1)+parseInt(int2)+parseInt(int3)
}())
}
var a= new obj(1,2,3);
console.log(a.add); // 6
Working fiddle :
https://jsfiddle.net/90ku8qae/
Update :
You forgot to add the closing bracket for the function obj. I just added and everything is working fine now.
I am testing an JS object
gl.test = function () {
var $ = jQuery;
var _private = null;
var _setStr = function ( t ) {
_private = t;
}
var _getStr = function() {
return _private;
}
var obj = {
init: function ( str ) {
_setStr(str);
},
test: function() {
console.log( _getStr() );
},
EOF: null
};
return obj;
}();
When executing the following:
var test1 = gl.test;
var test2 = gl.test;
test1.init('test1');
test2.init('test2');
test1.test();
test2.test();
the result comes back as test2 test2
This being an object I would expect for the order of execution not to matter and would expect for test1 and test2 to be returned as per:
var test1 = gl.test;
test1.init('test1');
test1.test();
var test2 = gl.test;
test2.init('test2');
test2.test();
Am I doing something wrong here?
As noted in the comments, you're not instantiating a new instance of gl.test. You could change it to not be an IIFE and use the new keyword
var gl ={}
gl.test = function () {
....
}
var test1 = new gl.test();
var test2 = new gl.test();
Live example: http://jsfiddle.net/19f9504u/
because test1 and test2 is the same object
// remove the autocall function and do this to gl.test
var test1 = gl.test();
var test2 = gl.test();
But it does not that how clases work in javascript by the way
I am trying to get two objects from the same class definition. However they seem to share the same attribute. What can i do?
http://jsfiddle.net/dagod/nuam8dks/2/
myclass = function() {
this.data.push(Math.random(1000));
};
myclass.prototype.data = [];
a = new myclass();
b = new myclass();
console.log(a.data);
console.log(b.data); //same as a.data
I've just been doing this for something else!
myclass = function() {
this.data = [];
};
Now you can access it my simply doing myclass.data =
Personally this is how i'd do it:
var MyNameSpace = {
SomeFunction: function() {
Some code
};
this.somevariable = somevalue;
};
Then you can go myNameSpace.myfunction() or myNameSpace.myVar = Value
See the comment from elclanrs.
var Myclass = function() {
this.data = [];
this.data.push(Math.random(1000));
};
You need to declare your member variable inside the constructor instead of making them part of the prototype. oop in javascript can be ugly and unintuitive. (Thats why there are so many oop libraries out there for javascript)
Using ds.oop
ds.make.class({
type: 'MyClass',
constructor: function(x){
this.a = x;
}
});
var c1 = new MyClass(1);
var c2 = new MyClass(2);
console.log( c1.a ); // output: 1
console.log( c2.a ); // output: 2
You can get the desired results as follows:
myclass = function() {
this.data = Math.random(1000);
};
//myclass.prototype.data = [];
var a = new myclass();
var b = new myclass();
jsfiddle
Look my problem:
I have a class that look like this:
var els=[];
var base = function(){
this.config = {}
}
var X1 = function(){
}
X1.prototype = new base();
X1.prototype.indexme = function(i){
this.config.index = i;
}
X1.prototype.add = function(){
var i = els.push(this)
this.indexme(i)
}
var teste = new X1();
teste.add();
var teste2 = new X1();
teste2.add();
var teste3 = new X1();
teste3.add();
console.log(els)
Why this.config.index is overwritten to another instances?
I expected that teste have config.index = 1; teste2 config.index= 2 and teste3 config.index=3
Thanks
All instances of X1 share the same prototype, which is an instance of base with a config property. Thus, all instances of X1 share the same config property. You can move the line this.config = {}; to the X1 constructor or you can define an init() function in base that assigns this.config for each object and call init() from the X1 constructor.
var ob = function(){
};
ob.prototype.func = function(){
};
var t = function(){
this.p=0;
this.function1(){
}
var a=new ob();
a.func=function(){//overrides the func
//hope to access this.p this.function1
}
};
is it possible to make a can access this.p this.function1 ?
Your comment welcome
You need to keep a reference to this from inside t if you want to access it within a.func. Try the following:
var t = function(){
var this_t = this; // Use this_t to access this.p and this.function1 inside a
this.p=0;
this.function1 = function(){
}
var a=new ob();
a.func = function(){//overrides the func
this_t.p = 1;
this_t.function1();
}
};