javascript console.log() improvement [duplicate] - javascript

This question already has an answer here:
how can I override console.log() and prepend a word at the beginning of the output?
(1 answer)
Closed 7 years ago.
Here is the example
var x = "foo";
console.log(x);
=> x: foo
is it possible to override console.log() / warp the console.log() / npm package / _ etc... can help ?

It's not possible to do what you've shown in the question, no, for the simple reason that the value of x is passed to the function. There is no connection from the argument that the function receives back to the x variable or the context in which the x variable exists, and so there's no way for console.log (or any replacement of it you would author) to determine that the name was, in fact, x.
Since console.log accepts multiple arguments, you can do:
console.log('x', x);
...which is a simple and easy way to get output similar to what you want.
You can also log a temporary object:
console.log({x:x});
In ES2015 (aka ES6) that can even just be:
console.log({x});

Related

_ as a variable in JavaScript function [duplicate]

This question already has an answer here:
Understanding arrow function parameters
(1 answer)
Closed 1 year ago.
I came across this function:
const rotatedTetro = matrix.map((_, index)=> matrix.map(col => col[index]))
What does the _ passed in mean?
There’s nothing special about underscore as a variable name. It’s just a convention often used to indicate that the argument won’t be used, but it needs to be present because you need the args that follow it.
It’s a way of indicating the code is “skipping over” that argument.

jquery is not functional? [duplicate]

This question already has answers here:
JavaScript function binding (this keyword) is lost after assignment
(5 answers)
Closed 5 years ago.
firefox (v52.0), jquery
this works:
// example 1
$('html').find('body')
this works:
// example 2
var h
h=$('html')
h.find('body')
This doesn't work:
// example 3
var f
f=$('html').find
f('body')
I get
Error: Permission denied to access property "ownerDocument"
why?
but this works:
// example 4
var a
a = x => $('html').find(x)
a('body')
Example 3 doesn't work because find is called on the global context when you assign it to f. If you use call and pass in a valid jQuery object as the context, the code works. Try this
var f = $('html').find;
console.log(f.call($('html'), 'body').length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Also, example 4 works because a can be translated to the following code, if written without an arrow function.
var a = function(x) {
return $('html').find(x);
};
It's just example 1, but with a wrapper function in order to take a parameter
No, jQuery is not functional
By looking at jQuery core source code:
https://github.com/jquery/jquery/blob/master/src/core.js#L51
You could see it hold state in this.
In f=$('html').find, the this of f is changed and no longer the this holding $('html') state
--
In this case, you are just proxy the method call. The this of find no change.
var a
a x => $('html').find(x)
a('body')

Javascript chained scope and context [duplicate]

This question already has answers here:
Execute JavaScript code stored as a string
(22 answers)
Closed 8 months ago.
Is it possible to define a function run that acts like this?
a = run('var x = 100;')
b = run('console.log(x);') // prints 100
c = run('y = 1;')
d = run('console.log(y);') // prints 1
I tried several ways, using apply and passing the same context, binding a context to a function, returning a closure with a recursive call etc. but I can't seem to get anything to work.
Yeah, as MyLibrary says, you probably want eval, if you really want to do this. So:
var run = eval;
a = run('var x = 100;')
b = run('console.log(x);') // prints 100
c = run('y = 1;')
d = run('console.log(y);') // prints 1
would seem to work.
JavaScript allows assigning functions to variables, so you can set the run variable to eval. As far as eval, you may want to learn about it and as you can see from comments, its use in normal function creation is often discouraged.
Are you referring to eval function?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Using String to get JSON Property [duplicate]

This question already has an answer here:
Accessing a JSON property (String) using a variable
(1 answer)
Closed 8 years ago.
I want to use a string as a JSON property in JavaScript.
var knights = {
'phrases': 'Ni!'
};
var x = 'phrases';
console.log(knights.x); // Doesn't log "Ni!"
When I run this code, it obviously doesn't work because it interprets "x" and not the contents of the variable "x".
The full code in context on pastebin: http://pastebin.com/bMQJ9EDf
Is there an easy solution to this?
knights.x looks for a property named x. You want knights[x], which is equivalent to knights['phrases'] == knights.phrases.
Full code (fixing a couple of typos in your example):
var knights = {
"phrases": "Ni!"
};
var x = 'phrases';
console.log(knights[x]); // logs Ni!
Try this to access using variables having string values
kinghts[x]
Basically this is trick
kinghts[x]==knighted["phrases"]==knighted.phrases.
knights.x will get a key named x, So it'll return undefined here.
knights.x is the same as knights['x'] - retrieving a property under the key x. It's not accessing the variable x and substituting in the value. Instead, you want knights[x] which is the equivalent of knights['phrases']

How do you get the string of a variable name in javascript? [duplicate]

This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 9 years ago.
I have a variable as such:
var foo = ["cool", "cool"];
How do I get the variable name, foo, as a string? In other words, given foo, how do I get "foo"? I want to use it in the following context where "foo" is used in getElementById(...):
document.getElementById("foo").innerHTML = document.getElementById("foo").innerHTML + "";
Thanks!
You're looking at this the wrong way round - to answer this question directly
given foo, how do I get "foo"?
Well, the answer is... type "foo" in your code.
You know you want the string representation of the foo variable name when you write your code, so there's no scenario where the string value you want would be anything other than "foo", right?
It's true that you can reference a property foo, for example of the window object, using window["foo"] but that's the other way round too - you have to know it's foo you want, and deliberately use "foo" to get at it. So there's an example - you know what string to use, "foo", at compile time - it couldn't possibly be anything else.
If it's a global variable, you get it as global object property: window in browsers and global in NodeJS. Otherwise, you don't.

Categories