问题
Is there a function in JavaScript that returns the default value for a type name that is passed to it? For example:
var defaultValue = built_in_getDefaultValue("number");
console.log(defaultValue); // Logs the number 0 to the console.
回答1:
Both of the answers correctly note that the default (unassigned) value of a variable in JavaScript is undefined, but neither addresses what you seem to be looking for—an equivalent of e.g. the default operator in C#.
Fortunately, JavaScript has a very simple type system: everything that isn't one of the six primitive types (five in ES5) is an object, and can be called via its constructor. The following function, given a string representing
- the name of a JS primitive;
- any valid production of the
typeofoperator; or - a function name either in the
thisvalue it is called with or its local scope¹
will produce a reasonable interpretation of a "default" value:
function defaultVal(type) {
if (typeof type !== 'string') throw new TypeError('Type must be a string.');
// Handle simple types (primitives and plain function/object)
switch (type) {
case 'boolean' : return false;
case 'function' : return function () {};
case 'null' : return null;
case 'number' : return 0;
case 'object' : return {};
case 'string' : return "";
case 'symbol' : return Symbol();
case 'undefined' : return void 0;
}
try {
// Look for constructor in this or current scope
var ctor = typeof this[type] === 'function'
? this[type]
: eval(type);
return new ctor;
// Constructor not found, return new object
} catch (e) { return {}; }
}
You can call it like this:
defaultVal( typeof 1 ); // -> 0
defaultVal( 'number' ); // -> 0
defaultVal( 'object' ); // -> {}
defaultVal( 'RegExp' ); // -> /(?:)/
Unfortunately, the JS typeof operator may be a bit… anameic for our purposes. For example, typeof null === 'object', and typeof [] === 'object', which probably isn't what you want.
To work around this, here's a function that returns the result of calling typeof on a value unless the result is 'object', in which case it will return 'null' if the value is null and obj.constructor.name otherwise (with 'object' as the fallback value if all else fails):
function getType(obj) {
var type = typeof obj;
if (type !== 'object') return type; // primitive or function
if (obj === null) return 'null'; // null
// Everything else, check for a constructor
var ctor = obj.constructor;
var name = typeof ctor === 'function' && ctor.name;
return typeof name === 'string' && name.length > 0 ? name : 'object';
}
And now we can have:
defaultVal( getType( 1234 ) ); // -> 0
defaultVal( getType( [99] ) ); // -> []
defaultVal( getType( 'ab' ) ); // -> ""
defaultVal( getType( null ) ); // -> null (Not possible with typeof!)
defaultVal( getType( /.*/ ) ); // -> /(?:)/ (Not possible with typeof!)
function T () {}
defaultVal( getType( new T ) ); // -> T {} (Not possible with typeof!)
I suspect this is as close to what you're looking for as you're going to get. Also, to head off any criticism: the use of eval above might seem a bit dirty, but it's the only way to get a named value from the current scope.
¹ This function can't call a constructor instantiated in another scope; instead, I've made it possible to call it with this bound to an object containing any such constructors it may reference:
function innerScopeCtor() {
function T () {}
return defaultVal.call( { T : T }, 'T' );
}
innerScopeCtor(); // T {}
If you don't do this, it will just fall back to returning a new object.
回答2:
I don't understand why do you ask such a question. Anyways, the default value of a variable in JavaScript is null or undefined.
For learning purposes, I took this from WikiBooks JavaScript/Variables and Types:
Variables are commonly explicitly declared by the var statement, as shown below:
var c;The above variable is created, but has the default value of
undefined. To be of value, the variable needs to be initialized:var c = 0;After being declared, a variable may be assigned a new value which will replace the old one:
c = 1;But make sure to declare a variable with var before (or while) assigning to it; otherwise you will create a "scope bug."
And now, when you ask why the value is not 0, it is because, even null or undefined are values, which are not defined. Not defined is different from being defined and empty. It might return 0, when the value is declared and not defined.
回答3:
The default value of any variable in javascript is undefined. You can also get it as
var defaultValue = void 0;
来源:https://stackoverflow.com/questions/14603106/default-value-of-a-type-in-javascript