This question already has answers here:
How do I call a JavaScript function on page load?
(8 answers)
why don't const and let statements get defined on the window object [duplicate]
(2 answers)
Closed 2 years ago.
I know is kind of a dumb question but I'm kinda confused, tried to look up on the internet but couldn't find anything so..
when in JS (ES6) I declare an arrow function as const, how can I invoke it when page is loaded?
es.
const bigBla = async () => { bla bla };
tried smt like onload="bigBla()" but doesn't work, also tried to invoke it at the end of the JS file but won't work in the HTML file (need to change innerHTML of a span element/src of an iframe, data come from http request), whereas if i declare function bigBla(){...} and use onload="bigBla()" i have no problems. Help me :(
Try window.onload function in your javascript file.
window.onload = function() {
bigBla();
};
Related
This question already has answers here:
When do I use parentheses and when do I not?
(5 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 1 year ago.
I was going through a Svelte tutorial where they bind a function to on:click in a button and they show how if you put () after the function name it doesn't work right, it runs once when the page loads and then when you click the button the function isn't ran. They said to remove the () from the function name so that you bind to a pointer to the function instead of the function itself. If you do that then it works as expected. I did further research and it looks like pointers to functions in JavaScript don't really exist. So what's going on here? Do pointers to functions exist or do they not exist, and what's the difference in Svelte when binding to a function that has () (what's that called?) after its name and a function that doesn't?
This question already has answers here:
How do I write an extension method in JavaScript?
(2 answers)
Add method to string class
(6 answers)
Closed 2 years ago.
I am kinda new to js and would appreciate some help to clarify one subject.
Basically i want to call some functions that i write like default javascript are called:
//declaring function
const splitAsExample = text => text.split('|')
//calling function
splitAsExample('Yesterday|Today|Tomorrow')
Instead of calling the function as mentioned above, i would like to know if it's possible to make a function that can be called like:
'Yesterday|Today|Tomorrow'.splitAsExample()
//and || or
'Yesterday|Today|Tomorrow'.splitAsExample
I learned js all by myself and didn't manage to find a specific name for this question to search up in google. :)
If you can clarify this topic for me it would be great, but if you could give me the name to search it up would be even better!
You could add a prototype function to String.
This allows method chaining with a given object.
String.prototype.splitAsExample = function () { return this.split('|'); };
console.log('Yesterday|Today|Tomorrow'.splitAsExample());
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 3 years ago.
I am working on some JavaScript code and I run into this syntax that I have never seen and I am trying hard to understand but cannot find good examples.
Can someone please describe what might be going on here?
function onMouseMove(event) {
(function(ev) {
// some piece of code
})(event);
}
This syntax is used to create an inner scope using a function and that function is immediately invoked with the event object.
This question already has answers here:
How can I read ‘native code’ JavaScript functions?
(3 answers)
Closed 3 years ago.
This may be a very dumb question.
I want to see the code of a function (Built in and User defined) in Javascript.
For example :
function hello(){
console.log("hello")
}
hello.toString() // Gives the function definition
'function hello(){\nconsole.log("hello")\n}'
Is there a way to see the native code like Math.random.toString()?
Update: From the comments, Seblor explained that native code cannot be seen.
You could do some string formatting to get a "better" look at your functions. Use this peace of code to get rid of the function name to get just the code.
function justGetCode(funcName)
{
var tempString = funcName.toString();
tempString = tempString.substring(tempString.indexOf("{"));
return tempString
}
But beyond this there is little you can do in terms of digging into native (i.e. browser specific ) code as it is encapsulated. This should work on library functions however.
Now i do not know what you are planning on doing with the returned function, but for fancier function manipulation you can always use in-built reflection mechanisms
This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.