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

javascript - passing string becomes object - Stack Overflow

programmeradmin0浏览0评论

I'm passing with marketing.reg("bum","a","b","c"); a string to a function.

In the first scope it is still a string.

After passing to the next scope it bees an object. How to avoid it?

    function mktg() {};
    mktg.prototype.reg = function(nameOfEvent,a,b,c) {
    //typeof a,b,c is string
            var fktn= function(a,b,c) {
                console.log(typeof a); //is object not string
                console.log( "fktn a: ",a);
            };

            $(document).on(nameOfEvent, fktn);

    };
    var marketing = new mktg;
    marketing.reg("bum","a","b","c");

I'm passing with marketing.reg("bum","a","b","c"); a string to a function.

In the first scope it is still a string.

After passing to the next scope it bees an object. How to avoid it?

    function mktg() {};
    mktg.prototype.reg = function(nameOfEvent,a,b,c) {
    //typeof a,b,c is string
            var fktn= function(a,b,c) {
                console.log(typeof a); //is object not string
                console.log( "fktn a: ",a);
            };

            $(document).on(nameOfEvent, fktn);

    };
    var marketing = new mktg;
    marketing.reg("bum","a","b","c");
Share Improve this question edited Aug 14, 2014 at 14:40 esqew 44.8k28 gold badges130 silver badges171 bronze badges asked Aug 14, 2014 at 14:38 hamburgerhamburger 1,4454 gold badges21 silver badges42 bronze badges 4
  • how is calling the fktn methos is the jquery event. – Callebe Commented Aug 14, 2014 at 14:40
  • But what's the use? You can still use it right? – Praveen Kumar Purushothaman Commented Aug 14, 2014 at 14:40
  • You are checking the type of the event object - which is an object. – ankr Commented Aug 14, 2014 at 14:41
  • I hope function(a,b,c) { these params are different from function(nameOfEvent,a,b,c).. – Raghuveer Commented Aug 14, 2014 at 14:50
Add a ment  | 

3 Answers 3

Reset to default 10

The variables a, b and c get replaced by the arguments of the event call "bum".

You should remove the paramters a, b and c from the function fktn.

That is because you are calling function fktn on some event.

So in your function

 var fktn= function(a,b,c) {}

The variables a,b,c are no more variables from function reg they are parameters of function fktn.

On every event the first called parameter will be the event itself which is an object, that's why you are getting typeof a is object.

instead of function (a,b,c)

try something like function ("'"+a+"', '"+b+"', '"+c+"'")

This puts the VALUES of a, b and c into quotes within the parameter list and therefore means they will be of type string.

Apologies if it's hard to read. It starts of with single quote inside double quotes, then concatenates the value of a, then concatenates with ', ' inside double quotes etc ..

发布评论

评论列表(0)

  1. 暂无评论