admin管理员组文章数量:1356827
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 fromfunction(nameOfEvent,a,b,c)
.. – Raghuveer Commented Aug 14, 2014 at 14:50
3 Answers
Reset to default 10The 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 ..
本文标签: javascriptpassing string becomes objectStack Overflow
版权声明:本文标题:javascript - passing string becomes object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744031760a2579039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论