第一个对象属性 [英] first object property
问题描述
我得到一个ajax / json响应,这是一个哈希/对象,
只有一个键。他们的关键'的价值决定了记录的种类
返回。我想要一个简洁的方法从
对象中获取* first *键,这是我想出的最好的
Object.prototype.first_property = function(){
var property;
for(此属性){break; }
返回属性;
}
({a:42,b:42})。first_property (); // =''''
有更简单的方法吗?
亲切的问候。
i''m getting an ajax/json response back which is a hash/object with
exactly one key. they key''s value determines the kind of record
returned. i wanted a concise way to grab the *first* key from an
object and this is the best i came up with
Object.prototype.first_property = function(){
var property;
for( property in this){ break; }
return property;
}
( { a : 42, b : 42 } ).first_property(); // =''a''
is there a simpler method of doing this?
kind regards.
推荐答案
Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:
-a写道:
-a wrote:
>将对象传递给方法
var first_property = function(obj){
var prop;
for(prop in obj){break}
return prop;
}
>passes the object in to the method
var first_property = function(obj){
var prop;
for(prop in obj){ break }
return prop;
}
[...]
[...]
>所以我再问一次 - 对于一个带有* one *属性的对象,检索所述属性名称的最简洁方法是什么?
>so i ask again - for an object with *one* property, what is the
cleanest way to retrieve said property''s name?
你的。
Yours.
更正:一个属性可以是不可枚举的,因此在该循环中不会显示
。
PointedEars
-
"使用任何版本的Microsoft Frontpage创建您的网站。 (这不会是b $ b阻止人们查看你的来源,但是没有人会想要窃取它。)"
- 来自< http:// www.vortex-webdesign.com/help/hidesource.htm>
Correction: That one property can be non-enumerable, and so would not show
up in that loop.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won''t
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
这对我来说似乎在ff中工作得很好,即
this seems to be working pretty well for me at the moment in ff, ie,
and safari [...]
and safari [...]
它不是因为它不能。
It does not because it can not.
你跑了吗?
我对理论上的讨论并不感兴趣,因为它非常>
在开放式,封闭式基于
的语言中,可以声称永远的一些事情都是正确的。但是,如果您能以任何直接的方式破解此代码,我将很高兴您展示它:
< html>
< head>
< script>
Object.prototype.own_properties = function(callback){
var props = [];
for(var prop in this){
if(this.hasOwnProperty(prop)){
props.push (道具)
尝试{
callback&&回调(道具);
}
catch(e){
if(e [''return'']){return e [ ''return'']}
else {throw e}
}
}
}
返回道具;
}
Object.prototype.first_property = function(){
返回此信息。 own_properties(function(prop){
throw({''return'':prop})}
);
}
Object.prototype.foo =''foo'';
Object.prototype.bar = function(){return''bar''};
var json_response_from_server =''{foo:bar}'';
var obj = eval(''(''+ json_response_from_server +'')'');
alert(''obj.own_properties():[''+ obj.own_properties()。join('',
'')+' '''');
alert(''obj.first_property():''+ obj.first_property());
< / script> ;
< / head>
< b ody>< / body>
< / html>
再次,我对这个为* all *对象工作的代码不感兴趣,只有
用于从服务器返回的对象,如上所述,具有已知的
格式的''{type:data}''
问候。
did you run it?
i''m not really interested in theoretical disscusions as there are very
few things which can be claimed to ''always'' be true in a open, closure
based language. however, if you can break this code in any
straightforward manner i''d be happy for you to demonstrate it:
<html>
<head>
<script>
Object.prototype.own_properties = function(callback){
var props = [];
for(var prop in this){
if(this.hasOwnProperty(prop)){
props.push(prop)
try{
callback && callback(prop);
}
catch(e){
if(e[''return'']){ return e[''return''] }
else{ throw e }
}
}
}
return props;
}
Object.prototype.first_property = function(){
return this.own_properties(function(prop){
throw({ ''return'' : prop }) }
);
}
Object.prototype.foo = ''foo'';
Object.prototype.bar = function(){ return ''bar'' };
var json_response_from_server = '' { foo : bar } '';
var obj = eval( ''('' + json_response_from_server + '')'' );
alert(''obj.own_properties(): ['' + obj.own_properties().join('',
'') + '']'');
alert(''obj.first_property(): '' + obj.first_property());
</script>
</head>
<body></body>
</html>
again, i''m not interested in this code working for *all* objects, only
for objects returned from a server as defined above with a known
format of ''{ type : data }''
regards.
Thomas''PointedEars''Lahn在8/14/2007 3:10 PM说了以下内容:
Thomas ''PointedEars'' Lahn said the following on 8/14/2007 3:10 PM:
-a写道:
^^^^^^^^
-a wrote:
^^^^^^^^
< snip>
<snip>
> http://www.google。 com / search?q = ara.t ... ient = firefox -a
不要太荒谬。
Don''t be ridiculous.
来自你,以及你把人们指向不合适的谷歌的习惯
搜索,它只能证明你的愚蠢和无法掌握现实。
< snip>
Coming from you, and your habit of pointing people to inadequate Google
Searches, it only proves your stupidity and inability to grasp Reality.
<snip>
请修剪你的报价。
PointedEars
Please trim your quotes.
PointedEars
请修改您的签名。
-
Randy
机会有利于准备好的心灵
comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html
Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/
这篇关于第一个对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!