views:

45

answers:

1

My code ise this:

var cem = { "name": "cem topkaya" };
f_PropertyBul(cem);
function f_PropertyBul(obj) {

    for (var prop in obj) {

        document.writeln(obj + " prop: " + prop + " propertyIsEnumerable:" + obj.propertyIsEnumerable(prop) + "<br/>");

        if (obj.propertyIsEnumerable(prop)) {            
            f_PropertyBul(obj[prop]);            
        }

    }
 }

I know there are a lot of question and answers about that but i didn't get why i get this result :

[object Object] prop: isim enumaret: true
cem topkaya prop: 0 enumaret: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
.
..
....

At the end, i am reaching the last property as a string. But it still says that it has propertyIsEnumareble true .

I just want to send an object and search an property name and its value. when it found i just want to break search and return back one property of my JSON object.

+1  A: 

Strings are enumerable. For example:

var str = "string"
for (var c in str) {
   console.log(str[c]);
}

Returns:

s
t
r
i
n
g

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable

This method can determine whether the specified property in an object can be enumerated by a for...in loop

If you want to exclude strings, add a check for typeof prop !== "string" in the if statement.

CD Sanchez
Strings characters aren't enumerable in all browsers. Also, `for...in` will return any properties added to the string or its prototype.
Andy E
@Andy E: Perhaps. But I believe strings are enumerable as per the ECMAScript spec. Also, since it was only for demonstration purposes, I didn't see the need for the hasOwnProperty check. The OP doesn't need the hasOwnProperty check either since propertyIsEnumerable should not work with inherited properties anyways. Which browsers specifically is it that strings aren't enumerable? Since I usually only have the latest browsers installed, I'm usually ignorant about cross browser issues and refer to the spec by default.
CD Sanchez
IE8 and lower don't support enumerable strings. It was only added to the ECMA-262 specification recently (5th edition), so older browsers based on 3rd edition won't support it, with the exception of Firefox, which introduced the feature. FWIW, IE9's platform previews support it. "Inherited" properties on the prototype chain *are* enumerable (feel free to test), unless you specifically define them to be non-enumerable (also ECMA-262 5th only).
Andy E
@Andy E: `propertyIsEnumerable` should not work with properties in the prototype chain as per the ECMA-262 spec. Section 15.2.4.7: "This method does not consider objects in the prototype chain." Recent versions of Firefox and Chrome seem to follow the specification. The following returns false in Chrome and FF: `({ prototype: { enum: [1,2,3] } }).propertyIsEnumerable("enum")`
CD Sanchez
@Daniel: A `for...in` will enumerate over custom properties added to the prototype chain, but not native functions and methods - they're not enumerable (aside from the odd bug in IE and Chrome). See http://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object/2258232#2258232. Any property or method that you add to the prototype chain (and there are a lot of String.prototype extensions) will be iterated over by a `for...in`.
Andy E
See also, http://jsfiddle.net/CcMYj/show/light/, where I add the property "test" to the String prototype chain... it shows up in the `for...in` loop (along with all the extra crap that jsFiddle throws in ;-))
Andy E
@Andy E: I think you misunderstood me. `propertyIsEnumerable` *specifically* does not work with inherited properties. If the OP does the "work" inside the `if` block for `propertyIsEnumerbale`, the `hasOwnProperty` check is redundant since it does not work with inherited properties.
CD Sanchez
@Daniel: yes, I misread that part and thought you were talking about `for...in` loops (as per your answer)
Andy E
@Andy E: Also, in ECMA 5th edition we'll finally be able to define non-enumerable properties (so they don't show up in `for..in` loops, inherited or otherwise) in objects using `Object.defineProperty` (it works in at least Chrome right now though). Though I figure you already knew that.
CD Sanchez
@Daniel: I did :) but I do think it's great. If only I could ditch writing for ECMA 3rd!
Andy E