Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:Method
};
})();
var value=Module.Method.doSomething;
console.log(value);
New to Javascript, coming from a java background.
Why is value coming back as undefined?
Cheers.
You have to have an object of Method to access the property doSomething, you can achieve it like
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:Method
};
})();
var value= new Module.Method();
alert(value.doSomething());
you can also approach it like
Module = (function () {
var Method = function () {
this.doSomething =function () {
return "doSomething";
};
}
return {
Method:new Method
};
})();
var value=Module.Method.doSomething();
alert(value);
Use as
var value= new Module.Method().doSomething();
DEMO
Module.Method is a function, apparently designed to be used as a constructor. You're also not calling the function.
var value=new Module.Method().doSomething();
^^^ create an instance of Module.Method
^^^ call the doSomething method of it.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is this a smart alternative to creating a object of functions? Benefits? Downsides?
var _ = require("underscore")
function functionName(fn){
//http://stackoverflow.com/a/17923727/340688
return /^function\s+([\w\$]+)\s*\(/.exec(fn.toString())[1]
}
function objecfify(arr){
return _.chain(arr)
.map(function(fn){
return [functionName(fn), fn]
})
.object()
.value()
}
var model = objecfify([
function create(){
return "create"
},
function read(){
return "read"
},
function update(){
return "update"
},
function remove(){
return "delete"
}
])
console.log(model)
Try to use fn.name to get function name:
var functions = [
function create(){
return "create"
},
function read(){
return "read"
},
function update(){
return "update"
},
function remove(){
return "delete"
}
];
var result = functions.map(function(fn) {
return fn.name;
});
document.write(JSON.stringify(result));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to do something really simple but not able to achieve this.
var Dummy = function() {
self = this;
setTimeout(function() {
if(self.render) {
self.render();
}
}, 10);
};
var test = new Dummy();
test.render = function() {
console.log('Make constructor call this method? ');
}
How can I make this work without settimeout?
Goal: Based on the function names attached to instance, I want to call them in particular order.
eg:
var Dummy = function() {
if(function1) {
function1();
}
if(function2) {
function2();
}
};
I am not sure what you are trying to do, but if what you want to do is call multiple functions (in order) when you instantiate an object instance of Dummy:
var Dummy = function() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === "function") {
arguments[i]();
}
}
}
var function1 = function() { console.log('Func 1 executed!'); }
var function2 = function() { console.log('Func 2 executed!'); }
var d = new Dummy(func1, func2, 'maybe a string also');
// log
'Func1 executed!'
'Func2 executed!'
You question isn't entirely clear, but I assume you have a function that you want to have called as soon as your object has been instantiated. The simplest way to do this is to pass the function as an argument.
Something like:
var Dummy = function(myFunction) {
self = this;
self.render = myFunction; // if you want to be able to call it again later
self.render(); // if you want to call it now
};
var test = new Dummy(function() {
console.log('Make constructor call this method? ');
});
var anotherTest = new Dummy(function() {
console.log("this instance will have a different render function");
});
Now, of course, this can be extended to handle any number of functions passed in:
var Dummy = function(func1, func2) {
// Note: you may want to check the arguments actually are functions
if (func1) {
func1();
}
if (func2) {
func2();
}
}
var test = new Dummy(function() { console.log("1"); },
function() { console.log("2"); });
Or you could pass in an array of functions in the order you want them executed:
var Dummy = function(funcArray) {
for (var i=0; i < funcArray.length; i++) {
funcArray[i]();
}
}
var test = new Dummy([function() { console.log("1"); },
function() { console.log("2"); }]);
Or you could pass in an object with the functions (and names) you want:
var Dummy = function(funcObject) {
if (funcObject.func1) {
funcObject.func1();
}
if (funcObject.func2) {
funcObject.func2();
}
}
var test = new Dummy({ func1 : function() { console.log("1"); },
func2: function() { console.log("2"); });
Becuase you are adding render() method on the Dummy instance (test) and you are not even calling that render() method.
I think you need to change to the following (assuming you don't want to use prototype to create the render() method):
var Dummy = function() {
this.render = function() {
console.log('Make constructor call this method called? ');
};
self = this;
setTimeout(function() {
console.log(self);
self.render();
}, 10);
};
var test = new Dummy();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have an Object
function Object(name, button) {
this.name = name;
this.button = button;
var alert = function () {
alert("x");
};
}
var ExampleOne = new Object('Example One', $('.exampleButton'));
How can I make the function inside the object fire on an event of Object[button]...
Using -
ExampleOne.button.click(function () {
ExampleOne.alert();
});
Doesn't work.
var alert = function is a local function and cannot be accessed outside that object. If you want it to be accessible with new instances, declare it with this:
function Object(name, button) {
this.name = name;
this.button = button;
this.alert = function () {
alert("x");
};
}
Fiddle: http://jsfiddle.net/qow3qu0m/2/
It's working for me. Check your code again. Maybe you forgot to include jQuery?
ExampleOne.button.click(function(e) {
alert('success!');
});
http://codepen.io/anon/pen/yyGpLz
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some functions, like:
function functionOne() {
/*
code
*/
}
function functionTwo() {
/*
code
*/
}
function functionThree() {
/*
code
*/
}
How can I write code to call each of the functions, once each (i.e. without repeats), in a random order?
Put your functions in an array, randomise the array and then iterate through it:
var functions = [
function () { alert("function 0"); },
function () { alert("function 1"); },
function () { alert("function 2"); },
function () { alert("function 3"); },
function () { alert("function 4"); }
];
functions.sort(function() { return 0.5 - Math.random() });
for (var i=0;i<5;i++) {
console.log(i);
functions[i]();
}
Here's a fiddle
this function should work:
function randCall(){
var array = [1,2,3];
while(array.length > 0){
var rand = (Math.random*array.length)|0; //converts to an Integer value
switch(array[rand]){
case 1:
functionOne();
break;
case 2:
functionTwo();
break;
case 3:
functionThree();
break;
}
array.splice(rand, 1);
}
}
hope this works and helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I encountered a problem that I can not seem to understand, why if define tick function like this;
function tick () { /* do something */ }
It works fine and in this mode
var tick = function () { /* do something */ }
doesn't work. Where is the problem?
working example of first http://jsbin.com/omokap/10/edit
working example of second http://jsbin.com/isobul/1/edit
The problem is that you are using tick before its definition in one case and after its definition in the other case.
In the first case:
force.on("tick", tick);
function tick () { /* ... */ }
the function tick is defined at parse time and is available to be passed as the second argument.
On the other hand in:
force.on("tick", tick);
var tick = function () { /* ... */ };
the variable tick is defined at parse time (so JSHint does not complain), but it gets its value only at runtime. Its value is undefined when force.on("tick", tick) is executed.
The difference is more apparent when you consider the following example:
var f;
if (true) {
f = function () { return 1; };
} else {
f = function () { return 2; };
}
f(); // returns 1
versus:
if (true) {
function f () { return 1; }
} else {
function f () { return 2; }
}
f(); // returns 2, from the latest definition
See this question to understand the difference between using the var tick = function () ... and function tick() ... better.