rules of using javascript GOTO , LABEL etc [duplicate] - javascript

This question already has answers here:
How can I use goto in Javascript?
(16 answers)
Closed 9 years ago.
Can anybody help me in understanding the use of goto , label etc in javascript, I have a program where I need to jump directly to a particular block of code...I am trying to use "break labelName" , but its showing an error that "labelName not found". Is there any rule to use label with break or continue
Thanks in advance .

You must be new to javaScript ;)
Anyhow: defining a function is defining your named excess point:
function doSomething(someVariable){
alert(someVariable);
}
So, somewhere else in your code you can now call doSomething('Hello world!');
Calling the function as above is the GOTO part and doSomething correlates to LABEL. As you see you can also pass some parameters (here it's someVariable) to the function (the executing code block) and work with it (like here the alert that causes an alert box showing in your browser showing the assigned value 'Hello world!'.
Maybe this also helps you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function or http://www.w3schools.com/js/js_functions.asp

Related

Transform alert to console.log [duplicate]

This question already has answers here:
JavaScript: Overriding alert()
(12 answers)
Closed 4 years ago.
Is it possible to change the alert() function to log the message instead of showing up a popup?
Some outside team members does like the use the alert function, but we don't want it because they aren't removing them.
So I want to add some code in our angular application that logs the message in the console instead of popping up.
So, I want some code which don't require to change all the calls to alert() but a kind of hook to that function which transforms it to console.log()
Try this
window.alert = window.console.log
window.alert('Hello from console.log')

How to unset chrome.windows.onRemoved.addListener? [duplicate]

This question already has an answer here:
`chrome.webRequest.onBeforeRequest.removeListener`? -- How to stop a chrome web listener
(1 answer)
Closed 6 years ago.
I have the following code in my Chrome extension to detect when windows are closed:
closeListener = chrome.windows.onRemoved.addListener(function(closed_window_id){
// something
}
How do I unset this such that the anonymous function does not fire? i.e. Something like:
chrome.windows.onRemoved.removeListener(closeListener)
ANSWER
Stephan/wOxxOms answer is correct. The function within the addListener cannot be anonymous and the removeListener syntax uses the function name (or a variable pointing to the function) to clear it.
Updated codepen: http://codepen.io/anon/pen/EgpNpz
After taking a look at your code, I see your problem. The function you put into the addListener is anonymous and needs to be set to a variable or become a named function.
function newListener() {
alert();
}
//This will add the listener
chrome.windows.onRemoved.addListener(newListener);
//This will remove it
chrome.windows.onRemoved.removeListener(newListener);

Error in Overriding alert with console.log function in javascript [duplicate]

This question already has answers here:
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Closed 6 years ago.
I am new to javascript. Due to certain reasons, I need to override windows.alert function by console.log function. For that, I have written following code,
window.alert = console.log;
alert('abc'); // gives TypeError : Illegal invocation.
I don't know what's wrong I am doing here. As per my knowledge, it's possible to modify javascript function reference with another function object.
Edit
Many have downvoted my question and given me reference to similar question, but
my problem is different. I want to suppress the warning of datatable grid library in jquery. That's why I want to replace alert by console.log or other function.
Hope this should work...
alert("msg");
function alert(msg){
console.log(msg);
}
This should do
var alert = (...params) => console.log(...params);

Javascript How do I force a string + variable to be evaluated as a variable [duplicate]

This question already has answers here:
is there a way to execute a function when I have its name in a string [duplicate]
(2 answers)
Closed 9 years ago.
Im not even sure how to word this and is probably why I am having trouble finding an answer in google.
When the code is run currentCardRow will equal 1 therefore it should be cardSelected1 which is what is shown in the console.log. I need it to go a step further because cardSelected1 is a variable and I need it to evaluate show in the console log as Invitation. Invitation is an example of a variable for cardSelected1.
I am not sure on what the correct syntax is to make this happen.
var currentCardSelected = "cardSelected" + currentCardRow;
Thanks for your help!
JavaScript has an Eval() function which allows you to evaluate strings as javascript code.
For example
var bar = "123";
var foo = "bar";
console.log(eval(foo));
will print "123" to the console.
For more information on eval, you can consult the MDN docs.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Generally, the use of eval() is considered poor practice as it makes the code difficult to read. There are likely more elegant solutions to implement what you have described, however, eval will solve your current problem.
var currentCardSelected = eval("cardSelected" + currentCardRow);
This is how my problem is fixed.

JavaScript - Get calling object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

Categories