Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6091821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:22:18+00:00 2026-05-23T12:22:18+00:00

I usually test to see if a string is a key in an object

  • 0

I usually test to see if a string is a key in an object by asking:

var foo:Object = {bar:"bar", bah:["bah","bah1"]};
var str:String = "boo";
if(foo[str]) // do something

But if str == “constructor” it will do something – because foo[“constructor”] returns true no matter what.

What is the best way to test if a string is the key of an object – and does not return true for constructor?

Some examples:

var foo:Object = {bar:"bar", bah:["bah","bah1"]};
trace('foo["bar"]: ' + foo["bar"]);
trace('foo["bah"]: ' + foo["bah"]);
trace('foo["constructor"]: ' + foo["constructor"]);
trace('foo["bar"] == true: ' + (foo["bar"] == true));
trace('foo["bah"] == true: ' + (foo["bah"] == true));
trace('foo["constructor"] == true: ' + (foo["constructor"] == true));
if(foo["bar"]){
    trace("foo:bar");
}
if(foo["bah"]){
    trace("foo:bah");
}
if(foo["constructor"]){
    trace("foo:constructor");
}
trace('"constructor" in foo: ' +  ("constructor" in foo));

Traces:

/*
foo["bar"]: bar
foo["bah"]: bah,bah1
foo["constructor"]: [class Object]
foo["bar"] == true: false
foo["bah"] == true: false
foo["constructor"] == true: false
foo:bar
foo:bah
foo:constructor
"constructor" in foo: true
*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:22:19+00:00Added an answer on May 23, 2026 at 12:22 pm

    If it’s a concrete class implementation, you should be able to get away with hasOwnProperty for the most part:

    var hasProperty : Boolean = foo.hasOwnProperty("bar");
    

    Which apparently should also work fine on an anonymous object (thanks for letting me know!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually never see test for new in C++ and I was wondering why.
I usually test my code locally on my work machine and then move it
During development, I usually test ASP.Net applications using the Web Development Server (sometimes called
A few days ago VS 2010 went in Beta test and usually with new
To be able to unit test my C++ code I usually pass the constructor
I have a unit test project based on UnitTest++. I usually put a breakpoint
I have read this post about how to test private methods. I usually do
I use Assert.Fail a lot when doing TDD. I'm usually working on one test
I see lots of TDD practitioners following this cycle: 1) Write your test as
So usually my apps are very small and my test framework is just a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.