javascript 如何在Javascript中将对象作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14652421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass object as a parameter in Javascript?
提问by ModernDesigner
I found a few questions on here relating to my question but didn't exactly get the answer I was looking for. I am wanting to do something like this, similar to what jQuery often does:
我在这里找到了一些与我的问题相关的问题,但没有完全得到我正在寻找的答案。我想做这样的事情,类似于 jQuery 经常做的事情:
createCSS("mystyle.css", {
media: "screen",
type: "text/css"
});
I tried this to accomplish what I want but it's not working:
我尝试这样做来完成我想要的但它不起作用:
var prop = {
media: '',
type: ''
};
function createCSS(href, prop) {
var anchor = document.createElement("link");
anchor.setAttribute("href", href);
anchor.setAttribute("media", prop.media);
anchor.setAttribute("type", prop.type);
if(typeof anchor != "undefined") {
document.getElementsByTagName("head")[0].appendChild( anchor );
}
}
Now, I know I could just create single multiple parameters like createCSS("mystyle.css", "screen", "text/css");
but I don't like that, the other way looks cooler.
现在,我知道我可以只创建单个多个参数,createCSS("mystyle.css", "screen", "text/css");
但我不喜欢那样,另一种方式看起来更酷。
Plenty new to javascript so any help would be very much appreciated!
javascript 有很多新内容,因此非常感谢您的帮助!
回答by bfavaretto
You don't have to declare/initialize var prop
. Your function looks fine, just call it passing an object as prop
, as in your own example:
您不必声明/初始化var prop
。您的函数看起来不错,只需调用它传递一个对象 as prop
,就像在您自己的示例中一样:
createCSS("mystyle.css", {
media: "screen",
type: "text/css"
});
If the intention with the var prop
part was to avoid assigning undefined
to the attributes, you need a little tweak inside the function:
如果var prop
零件的意图是避免分配undefined
给属性,则需要在函数内部进行一些调整:
function createCSS(href, prop) {
prop = (typeof prop !== "object") ? {} : prop;
prop.media = prop.media || 'screen'; // default media will be screen
prop.href = prop.href || 'text/css'; // default type will be text/css
// rest of code
}
Some minor improvements I'd suggest:
我建议一些小的改进:
- Your variable
anchor
does not contain an anchor (<a>
) element. Why not call itlink
? - You don't seem to need the
if(typeof anchor != "undefined")
conditional. since you're creating the element a few lines above, that variable will never be undefined. You can skip theif
andappendChild
directly.
- 您的变量
anchor
不包含锚点 (<a>
) 元素。为什么不叫呢link
? - 你似乎不需要
if(typeof anchor != "undefined")
条件。由于您正在创建上面几行的元素,因此该变量永远不会是未定义的。您可以直接跳过if
和appendChild
。
回答by Travis J
"I tried this to accomplish what I want but it's not working"
“我试过这个来完成我想要的,但它不起作用”
The way you pass your object is fine. Although there are some interesting choices in the function, it does what you are asking. However, have you inspected the link you create? It lacks the rel
directive which is a requirement.
您传递对象的方式很好。尽管该功能中有一些有趣的选择,但它可以满足您的要求。但是,您是否检查过您创建的链接?它缺少rel
作为要求的指令。
You may want to change your function to this:
您可能希望将您的功能更改为:
function createCSS(href, prop) {
var anchor = document.createElement("link");
anchor.setAttribute("href", href);
anchor.setAttribute("media", prop.media);
anchor.setAttribute("type", prop.type);
anchor.setAttribute("rel", prop.rel);
document.getElementsByTagName("head")[0].appendChild( anchor );
}
var prop = {
media: "screen",
type: "text/css",
rel: "stylesheet"
}
createCSS( "mystyle.css", prop );