0

Ive learned on MDN that an object is something with properties and method and that mostly everything in JavaScript is / can be treated as an object. Everything mentioned in the previous sentence makes it hard to tell what an object is. Some articles say that objects are just data containers others say that objects are neat and tidy packages of information about something you want to use in your code. Are the only objects: arrays, object literals, and functions?

let objectLiteral = {

} ;


let exFunction = () = > {

}


let arr = [

] ;
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Yes, nearly all values in JavaScript are objects. Yes, objects can be data containers. Yes, objects can be neat and tidy packages of information. Arrays are a special kind of object that can be initialized with the `[...]` syntax. Functions are a special kind of object that can be called with the `fn()` syntax and are declared with their own special syntax as well. There special cases like `undefined` which are not objects, arrays, or functions – p.s.w.g Apr 23 '19 at 22:25

3 Answers3

0

Objects are useful when you want to have something of many types. For instance, if you would like to have some information about a person, or contact; without objects, you would need to store the information as:

const name = 'John';
const age = 32;
const lastName = 'John Doe'
const zips = [1, 2, 3, 4]

however, objects let you have all those properties in a single type for instance:

const person = {
  name: 'John',
  age: 32,
  lastName: 'Doe'
  zips : [1, 2, 3, 4]
};

console.log(person.name) 

Objects con store anything, any type. I hope this helps.

German
  • 557
  • 3
  • 5
0

A JavaScript object is a name:value pair. When your web browser is communicating with a web server, more than likely the two are communicating over HTTP using JSON (JavaScript Object Notation)

Let's say you are viewing your facebook profile, when you click on the profile details, you request a page and the facebook server will serve you an object that might look like this:


{
  name: "iamlearningtocode"
  occupation: "student programmer"
  hobbies: "programming"
}

Then through the power of JavaScript, code is ran that takes that information and displays it on the page in a neat and tidy order. Hope this makes sense!

HellowThar
  • 83
  • 1
  • 7
0

An object is a complex data type, that stores values and lets them be accessible with a specific key. A key/value combination inside an object is called a property.

JavaScript is an object-oriented programming language. This means that nearly every value is an object. Some kinds of values are called primitives, and they are seen as the "foundation", as simple data types. These primitives are strings, numbers, booleans, null, and undefined.
(Note that apart from booleans, null and undefined, all primitives are secretly objects as well, because only objects can have methods - you have String.split, Number.toString, etc.)

There are also many other data types, which are a specific kind of object:

  • Arrays store values in an indexed fashion, and guarantee order (unlike objects)
  • Functions receive values, execute code, and may return a value for later use
  • Sets are similar to an array, but store only unique values

An object literal, however, is what many people think of when they think "object". It looks like this:

const myObject = {
  name: "Jack",
  cool: false,
  friends: 0
  pets: ["Dog", "Cat", "Snake"]
};

You can see that object properties are in the format of key: value, with a comma , afterwards if there are more properties (in ES6, trailing commas are allowed).

Objects can also contain methods:

const myObject = {
  name: "Jack",
  sayHello: function() {
    console.log(`Hello, my name is ${this.name}!`);
  }
};

Note: When making methods within an object, the highest-level function (in this case, the part directly after sayHello), must be an ES5-style function in order for references to this to function correctly. If nested functions (like setTimeout) need to refer to the object as well, they must be ES6-style functions, because arrow functions (=>) do not carry their own this binding.

Objects can also be constructed using constructors (ES5) or classes (ES6).

A constructor needs to be an ES5 function, and within it, you assign all the properties (usually from passed parameters):

function MyObject(name, cool) {
  this.name = name;
  this.cool = cool;
}

var jack = new MyObject("Jack", false);
var bob = new MyObject("Bob", true);

console.log(jack);
console.log(bob);
.as-console-wrapper { max-height: 100% !important; top: auto; }

To attach methods to new objects, you can attach them to the prototype of the constructor:

function MyObject(name, cool) {
  this.name = name;
  this.cool = cool;
}

MyObject.prototype.sayHello = function() {
  console.log(`Hello, I am ${this.name}`);
};

var jack = new MyObject("Jack", false);
var bob = new MyObject("Bob", true);

jack.sayHello();
bob.sayHello();

ES6 classes are a restyling of the constructor. You have a constructor function, where you define all your properties, and you place all your other methods in the same place. Here's our MyObject constructor transformed into an ES6 class:

class MyObject {
  constructor(name, cool) {
    this.name = name;
    this.cool = cool;
  }
  sayHello() {
    console.log(`Hello, I am ${this.name}`);
  }
}

let jack = new MyObject("Jack", false);
let bob = new MyObject("Bob", true);

jack.sayHello();
bob.sayHello();

You can also place objects into other data containers - like objects and arrays. If you place an object inside an object, you have a nested object:

const parent = {
  nested: {
    value: "I am nested"
  }
};

console.log(parent.nested.value);

As you can see above, you access different properties and methods of objects by using dot notation. But what if you have a property you want to get, but the property name is stored in a variable? You need to use dynamic property notation, which is also called square bracket notation (because it involves square brackets []):

const obj = {
  aReallyObscureKey: "Hello!"
};

const key = "aReallyObscureKey";

console.log(obj[key]);

Arrays can also hold objects:

const arrayOfObjects = [{
  name: "Jack",
  cool: false
}, {
  name: "Bob",
  cool: true
}];

console.log(arrayOfObjects);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Hopefully this helps. Here's a few links to help learn more about objects:

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79