最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript function callapply with string - Stack Overflow

programmeradmin5浏览0评论

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.

Share Improve this question asked Feb 22, 2013 at 15:38 TengizTengiz 8,44934 silver badges40 bronze badges 10
  • 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 the window object in there. Or better yet, Why do you need to check if this 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
 |  Show 5 more ments

4 Answers 4

Reset to default 6

Primitive 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:

  1. Else if Type(thisArg) is not Object, set the ThisBinding to ToObject(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/

发布评论

评论列表(0)

  1. 暂无评论