New Function()' with Lower Case "F" in JavaScript

`new function()` with lower case f in JavaScript

I've seen that technique before, it's valid, you are using a function expression as if it were a Constructor Function.

But IMHO, you can achieve the same with an auto-invoking function expression, I don't really see the point of using the new operator in that way:

var someObj = (function () {
var instance = {},
inner = 'some value';

instance.foo = 'blah';

instance.get_inner = function () {
return inner;
};

instance.set_inner = function (s) {
inner = s;
};

return instance;
})();

The purpose of the new operator is to create new object instances, setting up the [[Prototype]] internal property, you can see how this is made by the [Construct] internal property.

The above code will produce an equivalent result.

How Can I Use Caps In My Function? (Javascript)

Try this

let alph = "abcdefghijklmnopqrstuvwxyz";
alph += alph.toUpperCase();

.toLowerCase not working, replacement function?

The .toLowerCase() function only exists on strings.

You can call .toString() on anything in JavaScript to get a string representation.

Putting this all together:

var ans = 334;
var temp = ans.toString().toLowerCase();
alert(temp);

Convert JavaScript String to be all lower case

var lowerCaseName = "Your Name".toLowerCase();

Javascript Method Naming lowercase vs uppercase

A popular convention in Javascript is to only capitalize constructors (also often mistakenly called "classes").

function Person(name) {
this.name = name;
}
var person = new Person('John');

This convention is so popular that Crockford even included it in its JSLint under an optional — "Require Initial Caps for constructors" : )

Anything that's not a constructor usually starts with lowercase and is camelCased. This style is somewhat native to Javascript; ECMAScript, for example (ECMA-262, 3rd and 5th editions) — which JavaScript and other implementations conform to — follows exactly this convention, naming built-in methods in camelcase — Date.prototype.getFullYear, Object.prototype.hasOwnProperty, String.prototype.charCodeAt, etc.

Convert All String Values of an Object to Lowercase Javascript

You need to use .map() to iterate over the array, you can't just use the array itself as a function.

You can then use Object.entries(x).map() to iterate of the keys and values of the object, and lowercase the string values. A new object can then be created using Object.fromEntries().

let data = [{
userId: 1234567890,
errorId: 957,
userCategory: "Category",
userAge: 18,
userType: "Standard"
},
{
userId: 1234567890,
errorId: 583,
userCategory: "Second",
userAge: 28,
userType: "Superuser"
},
{
userId: 1234567890,
errorId: 823,
userCategory: "Third",
userAge: 38,
userType: "Admin"
}
]

let newData = data.map(x => Object.fromEntries(Object.entries(x).map(
([key, value]) => [key, typeof value == 'string' ? value.toLowerCase() : value])));

console.log(newData)

Given a string describing a Javascript function, convert it to a Javascript function

There's also the Function object.

var adder = new Function("a", "b", "return a + b");


Related Topics



Leave a reply



Submit