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
Related
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:
Set document.getElementById to variable
(5 answers)
Closed 5 years ago.
I have this habit of using function id(_id) { return document.getElementById(_id); } as to lessen the work of typing this over and over.
sidenote:
jquery becomes too heavy for small tasks and projects
As I learnt Javascript more I came to know that we can pass around functions just like objects so we can do
var id = document.getElementById;
It seems like both do the exact same thing. However, I am not an expert in Javascript, so could there be any difference internally? and which is the preferred way of using?
Yes, the context is:
const log = console.log;
log("test");
The upper wont work because the context changed, and log must be called on a console object, not on window ( aka without context). However, there are easy workarounds:
const log = console.log.bind(console);
Or you use functions, for example the arrow ones:
const log = (...args) => console.log(...args);
This question already has answers here:
How to get function name in strict mode [proper way]
(2 answers)
argument.callee.name alternative in the new ECMA5 Javascript Standard [duplicate]
(4 answers)
Closed 5 years ago.
'use strict';
function foobar(){
console.log(arguments.callee.name); // foobar
}
I hear this once worked, but now it's deprecated. There seem to be other alternative to it for other situations, and I've seen lots of references to using function.name, but having to enter foobar.name defeats the purpose. Within a class I can use this.constructor.name and get the name of the class. I'm looking for something like that for the name of a function.
Why this is not a duplicate to any existing question since ES6: one answer to one similar question talks about using named functions, but this is a named function. To another question the answer is to use function.name - but this question is basically asking if there is a way to .log the name without knowing the name. To another question the answer was to use something like ()=>Function.caller.name but that is non-standard.
If you're using ES6, the function now has a property 'name'
Using your example:
function foobar(){
console.log(foobar.name); // foobar
}
This question already has answers here:
Calling function inside object using bracket notation
(2 answers)
Closed 6 years ago.
I rather have a seemingly trivial issue, but am not able to figure out an efficient approach.
I have a list of about 50 functions to be called such as :
globalClient.funcA(...)
globalClient.funcB(...)
globalClient.funcC(...)
My code should ideally dynamically create the name of the function (funcA / funcB/ funcC and then proceed to actually call that function. My approach below does not work (please note that these aren't exactly the actual names of the functions. I'm only giving these arbitrary names for simplicity of understanding):
var functionName = 'func'.concat('A');
globalClient.functionName
The second line is where it errors out. Now JS thinks that functionName itself is the name of the function. What I want it to do is resolve functionName to funcA and then call globalClient.funcA(...) instead.
I've thought about implementing a switch / case for this but I'm sure there is a far simpler appraoch. Any ideas?
You could use the bracket notation as property accessor.
globalClient[functionName]()
You can use the [ ] operator for accessing the properties.
var globalClient = {
funcA: function(){
console.log('funcA is called');
}
}
var functionName = 'func'.concat('A');
globalClient[functionName]();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?
I use in my scripts:
(function(){
...[code]..
})()
I can't find documentation about this, i have see scripts when this form take args.
(function(){
...[code]..
})(arg1,arg2)
Somebody have a link or a good explanation about this javascript function?
this is just regular javascript.
you instanciate an anonymous function, and then call it with 2 arguments.
The confusing part I think is the on-the-fly aspect of the operation.
You could have done (at higher cost):
var hnd = function() {...};
hnd(arg1,arg2);
It's known as a Self-Executing Anonymous Function.
Here is the first Google result, which gives a solid overview.