object Object


Javascript Basic

object Object is Object which have no inheritance.

How to use Object API
Object Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

What is the usage diffrence between two methods?

//Object.keys()
var arr = ["a", "b", "c"];
console.log('Object.keys(arr)', Object.keys(arr)); // return key values.
//Object.prototype.toString()
var o = new Object();
console.log('o.toString()', o.toString());

in case of second methods, only Object can use the method.

Object extension

Object.prototype.contain = function(neddle) {
    for(var name in this){
        if(this[name] === neddle){
            return true;
        }
    }
    return false;
}
var o = {'name':'euido', 'city':'seoul'}
console.log(o.contain('euido')); //true
var a = ['euido','leezche','grapittie'];
console.log(a.contain('leezche')); //true

be careful when you extend Object

Object.prototype.contain = function(neddle) {
    for(var name in this){
        if(this[name] === neddle){
            return true;
        }
    }
    return false;
}
var o = {'name':'euido', 'city':'seoul'}
var a = ['euido','leezche','grapittie'];

for(var name in o){
  if(o.hasOwnPropery(name)){
    console.log(name);
  }
}
// the reason why we need to use hasOwnPropery function is
// if o has also contain methods so it shows for loop as well.





© 2017. by isme2n

Powered by aiden