I just noticed that, when I want to pass string as "this"
, the type cannot be obtained correctly inside a JavaScript function.
Here is an example:
var str = 'string value';
if (typeof (str) == 'string') {
alert('string outside');
}
var fn = function(s) {
if (typeof (str) == 'string') {
alert('string param');
}
if (typeof (this) == 'string') {
alert('string this');
}
else {
alert(typeof(this));
}
};
fn.call(str, str);
I see 3 messages: "string outside"
, "string param"
, and "object"
.
My goal is to write an "if"
statement that says "this"
is string. Something like if (typeof(this) == 'string')
. This one does not work, please point me to the correct statement that will work inside the function.
I just noticed that, when I want to pass string as "this"
, the type cannot be obtained correctly inside a JavaScript function.
Here is an example:
var str = 'string value';
if (typeof (str) == 'string') {
alert('string outside');
}
var fn = function(s) {
if (typeof (str) == 'string') {
alert('string param');
}
if (typeof (this) == 'string') {
alert('string this');
}
else {
alert(typeof(this));
}
};
fn.call(str, str);
I see 3 messages: "string outside"
, "string param"
, and "object"
.
My goal is to write an "if"
statement that says "this"
is string. Something like if (typeof(this) == 'string')
. This one does not work, please point me to the correct statement that will work inside the function.
- what is the end game? – Daniel A. White Commented Feb 22, 2013 at 15:40
- I want to know, if "this" is string. I don't have param in the real code, just "this", so no alternative. – Tengiz Commented Feb 22, 2013 at 15:40
-
What exactly are you trying to achieve with that?
this
is thewindow
object in there. Or better yet, Why do you need to check ifthis
is a string? It'll never be a string. – Cerbrus Commented Feb 22, 2013 at 15:41 - I want to know, if the caller intended to pass string (even though now that looks as object with characters inside). – Tengiz Commented Feb 22, 2013 at 15:44
-
why do you want to know that
this
is a string? – Daniel A. White Commented Feb 22, 2013 at 15:45
4 Answers
Reset to default 6Primitive values are embedded as objects when they're used as context.
From the MDN on the call function :
Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
If you want to know if the object is of type String, use :
var isString = Object.prototype.toString.call(str) == '[object String]';
This is the solution the MDN remends for object type detection.
ES spec forces the this
keyword to reference an object:
- Else if Type(thisArg) is not Object, set the
ThisBinding
toToObject(thisArg)
.
One workaround using Object.prototype.toString
:
Object.prototype.toString.call( this ) === '[object String]'
Fiddle
To access a function's parameters, you don't use this
.
Try this instead:
var fn = function(s) {
if (typeof (s) == 'string') { // "s" is your passed parameter there.
alert('string param');
}
};
(Apparently, you even specified a name for the passed parameter, already).
Have a look at this tutorial for the basics about functions and parameters.
The reason that typeof this
is "object"
is because this
must always point to an object. Here JavaScript is implicitly coercing the string to an object. What you want instead is the primitive value of the object. Try this:
var str = "string value";
if (typeof str === "string") alert("string outside");
function fn(str) {
var type = typeof this.valueOf();
if (typeof str === "string") alert("string param");
if (type === "string") alert("string this");
else alert(type);
};
fn.call(str, str);
Hope this helps. See the demo: http://jsfiddle/BuZuu/