object interview questions
Top object frequently asked interview questions
How do I check if a particular key exists in a JavaScript object or array?
If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
Source: (StackOverflow)
I have an object x
. I'd like to copy it as object y
, such that changes to y
do not modify x
.
What's the most elegant way of doing this in JavaScript?
Edit:
I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted properties. This isn't a problem, since I'm copying one of my own, literal-constructed objects.
Source: (StackOverflow)
The interviewer asked - Can we instantiate an abstract class? I said, No. He told me - Wrong, we can.
I argued a bit on this. Then he told me to try this yourself at your home.
abstract class my {
public void mymethod() {
System.out.print("Abstract");
}
}
class poly {
public static void main(String a[]) {
my m = new my() {};
m.mymethod();
}
}
Here, I'm creating instance of my class and calling method of abstract class. Can anyone please explain this to me? Was I really wrong during my interview?
Source: (StackOverflow)
What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o));
being used, but that's currently Firefox-only. In Mootools 1.2, I've done things like obj = JSON.parse(JSON.stringify(o));
but question the efficiency.
I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists.
Source: (StackOverflow)
Possible Duplicate:
How do you determine equality for two JavaScript objects?
What is the best way to compare objects in JavaScript?
Example:
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
I know that two objects are equal if they refer to the exact same object, but is there a way to check if they have the same attributes' values?
The following way works for me, but is it the only possibility?
var eq = Object.toJSON(user1) == Object.toJSON(user2);
alert(eq); // gives true
Source: (StackOverflow)
I've got an array:
myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]
I'm unable to change the structure of the array. I'm being passed an id of 45
, and I want to get 'bar'
for that object in the array.
How do I do this in JavaScript or using jQuery?
Source: (StackOverflow)
How can I convert a JavaScript object into a string?
Example:
var o = {a:1, b:2}
console.log(o)
console.log('Item: ' + o)
Output:
Object { a=1, b=2} // very nice readable output :)
Item: [object Object] // no idea what's inside :(
Source: (StackOverflow)
I am learning the ropes in Python. When I try to print an object of class Foobar
using the print()
function, I get an output like this:
<__main__.Foobar instance at 0x7ff2a18c>
Is there a way I can set the printing behaviour (or the string representation) of a class and its objects? For instance, when I call print()
on a class object, I would like to print its data members in a certain format. How to achieve this in Python?
If you are familiar with C++ classes, the above can be achieved for the standard ostream
by adding a friend ostream& operator << (ostream&, const Foobar&)
method for the class.
Source: (StackOverflow)
How can I sort this array of objects by one of its fields, like name
or count
?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....
Source: (StackOverflow)
checking for existence of javascript object property with a variable as part of the object name.
var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";
if(myObj.myProp){
alert("yes, i have that property");
};
This is undefined because it's looking for myObj.myProp when i want it to check for myObj.prop
Source: (StackOverflow)
Why is null considered an object in JavaScript?
Is checking
if ( object == null )
Do something
the same as
if ( !object )
Do something
?
And also:
What is the difference between null and undefined?
Source: (StackOverflow)
I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
Source: (StackOverflow)
I am currently using the isNaN function to check if my variable is a string or an object. I just wondered if this is the wrong way of doing it because it does not seem to be working.
if(isNaN(element))
element = document.querySelector(element);
At the moment even if element is an object it is still causing isNaN to return true. Any ideas? I am sure I am missing something obvious. Is it that isNaN only works for a string/integer test?
Source: (StackOverflow)
I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.
For example:
>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
Should be accessible in this way:
>>> x = dict2obj(d)
>>> x.a
1
>>> x.b.c
2
>>> x.d[1].foo
bar
I think, this is not possible without recursion, but what would be a nice way to get an objectstyle for dicts?
Thank you in advance.
Source: (StackOverflow)