3

I am making an ajax call using jquery to a PHP script that sends back a json encoded string as an object.

{
   "errors":{
      "name":"Name is empty",
      "gender":"Gender is empty"
   },
   "success":false,
   "message":"FAIL"
}

This is the json returned and since I'm using dataType: 'json' within my ajax call it is converted to an object which I have then console logged to observe.

I see that key/value pairs are in there but also something (prototypes) that wasn't a part of the json string. What is it and where did come from? Are these functions I could use?

I am attaching a screenshot as typing it would mess it up.

enter image description here

nshunz
  • 253
  • 1
  • 11

2 Answers2

6

Don't worry, you can ignore it.

JavaScript uses prototypical inheritance: An object inherits features from another object, called its prototype (which may, in turn, have its own prototype, and so on). It's possible to create an object that doesn't have a prototype, but by default, all objects do.

Plain objects in JavaScript inherit from the basic object prototype (which you can access via the Object.prototype property): When your JSON is parsed and the plain objects for its contents are created, their prototype is that object, which provides the toString, valueOf, and hasOwnProperty, and other methods and (on browsers) the __proto__ property (though you should pretend that last one isn't there):

const json = "{}";
const obj = JSON.parse(json);
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true

The <prototype> text in your screenshot is just that console's way of showing you the prototype of the object, there isn't really a property with that name. (In standard JavaScript, an object's prototype isn't referenced by any property, just an internal slot called [[Prototype]] you can only access via the Object.getPrototypeOf method. But on web browsers, there's a specified extension, __proto__, for it.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Prototypes in javascript are used as a form of inheritance. Javascript object inherit from other objects. Prototypal inheritance is used for property lookup and works in the following manner:

When an property of an object gets looked up it will first look at its own properties, if it can't find the property there it will check its prototype(s) if the property can be resolved there.

For example:

let obj = {prop1: 1};

console.log(obj.valueOf());

// here we implement the valueOf method on the object
obj.valueOf = function () { console.log('here I implemented valueOf myself') }

// now the own implementation is used because a property on the object
// has priority over a property on the prototype
obj.valueOf();

Prototypes don't affect your code in any way, it just adds functionality you can use out of the box. For example the valueOf method in the above code is located on Object.prototype and is available to all objects which inherit from it.

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155