Inherit nearly all from a first function [closed] - javascript

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 5 years ago.
Improve this question
Hello I'm trying to make DRY code (not repeat code) from the a secondFunction that inherits NEARLY all the content from a firstFunction.
This would be the example of what I want but it's not DRY:
function firstFunction(){
this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"
this.targetVariableToRemove = "something"
return [this.arrayObjectsToElastic]
}
function secondFunction(){
this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"
return [this.arrayObjectsToElastic]
}
Therefore, I don't want to "inherit" in the secondFunction the targetVariableToRemove from the firstFunction because if so it'll crash in some other processes I'm running.

Maybe you can do something like:
function secondFunction(){
return firstFunction().concat(["newContent"]);
}

Related

how to write a complex form of setTimeOut and setTimeInterval with and without recursion? [closed]

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 4 months ago.
Improve this question
function sayHello() {
alert('Hello');
}
setTimeout(sayHello, 1000);
This is a basic setTimeout example, but can I get an in-depth explanation like a long one how it works with recursion and without recursion in javascript? (visualization helps as snippet code too)
You can call setTimeout() again inside the callback. This will print Hello in 1 second, then print World 2 seconds after that.
function sayHello() {
console.log('Hello');
setTimeout(sayWorld, 2000);
}
function sayWorld() {
console.log('World');
}
setTimeout(sayHello, 1000);

How can I detect if user has successfully alerted a thing (I'm making an XSS game) [closed]

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 2 years ago.
Improve this question
The page just echoes the user input using GET. I have no idea on what concept to apply to detect if the user has alerted something
The easiest approach would probably be to replace the alert method with your own.
const log = [];
alert = function(alert) {
return (value) => {
log.push(value);
alert(value);
};
}(alert);
alert("Hello!");
console.log(log);

Show results of multiple prompt boxes using Javascript [closed]

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.
Improve this question
How to make multiple prompt boxes and sort their input alphabetically and then show on screen using JavaScript ?
Thanks
Sequentially
if you are talking about the almost obsolete native javascrtipt function prompt() then:
var array=[];questions=['A?','B?','C?'],current=0;
function q(question){
array.push(prompt(question,'write here')+' question number:'+current);
if(current<questions.length){
next()
}else{
array.sort()// needs a proper sort function
alert(array.join("\n"));
};
}
function next(){
q(questions[current++]);
}
next();
Demo
http://jsfiddle.net/c7GnX/

How do I catch the response from a named function? [closed]

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 9 years ago.
Improve this question
I'm calling a function like this:
$('.qa-nav-main-ask a').click(showAskForm);
Show showAskForm is this (simplified):
function showAskForm(){
return true;
}
I want to catch the response from showAskForm and save it to a variable, I tried
askFormShowing = $('.qa-nav-main-ask a').click(showAskForm);
without success.
function showAskForm(){
return true;
}
var askFormShowing;
$('.qa-nav-main-ask a').click(function(){askFormShowing = showAskForm();});
Try the above
return is a reserved word and cannot be used as a JavaScript identifier. So, use like this:
$('.qa-nav-main-ask a').click(showAskForm);
function showAskForm(){
myvar = true;
}

Use a method in a callback [closed]

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 9 years ago.
Improve this question
Question is being edited. Sorry for changes. Please don't provide answers at the moment :)
If I declare an object in javascript:
var MyObject = {
function myFunction() {
console.log('hi!');
}
}
Why do I get errors?
Wouldn't it rather be:
var MyObject = {
myFunction : function(){
console.log('hi!');
}
}
How to call the method:
MyObject.myFunction()

Categories

Resources