Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q31 Loose object equality #69

Open
QingYun opened this issue Jan 27, 2019 · 1 comment
Open

Q31 Loose object equality #69

QingYun opened this issue Jan 27, 2019 · 1 comment

Comments

@QingYun
Copy link

QingYun commented Jan 27, 2019

In Q31, we have

function Clone(object){
  var newObject = {};
  for(var key in object){
  	newObject[key] = object[key];
  }
  return newObject;
}

var objectLit = {foo : 'Bar'}; 
	var cloneObj = Clone(obj); // Clone is the function which you have to write 
	console.log(cloneObj === Clone(objectLit)); // this should return false
	console.log(cloneObj == Clone(objectLit)); // this should return true
  1. where does the obj come from in var cloneObj = Clone(obj)?

  2. should A == B be the same as A === B when both A and B are objects? why the last two lines have different outputs?

Thanks.

@ghost
Copy link

ghost commented Mar 9, 2019

Isn't that Clone() creates a new object but just has the same properties?
For the objects to be the same two objects are equal if they refer to the exact same object.
When you do cloneObj === Clone(objectLit) or cloneObj == Clone(objectLit) both will return false because they are simply identical but they are not the same object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant