“objeto em javascript” Respostas de código

objeto dentro do objeto JavaScript

var obj = {
  prop1: 5,
  obj2: {
    prop1: [3, 6, 3],
    prop2: 74,
    prop3: {
      str: "Hello World"
    }
  }
};

console.log(obj.obj2.prop3.str); //output: "Hello World"
TC5550

Objetos javascript

const objectName = {
  member1Name: member1Value,
  member2Name: member2Value,
  member3Name: member3Value
};
Itchy Iguana

Objetos em JavaScript

var student = {                 // object name
firstName:"Jane",           // list of properties and values
lastName:"Doe",
age:18,
height:170,
fullName : function() {     // object function
   return this.firstName + " " + this.lastName;
}
}; 
BlueMoon

Objeto JavaScript

/*
An object is made of key value pairs. Keys can be strings 
(which don't require quotes), array with a string, or symbols. Values can be arrays,
objects, numbers etc
*/

let testSymbol = Symbol('item number 3')

const obj = {
	item1: 1,
    "item number 2": 2,
    testSymbol: 3,
  	['a']: 4
}

// Another way of creating an object:
const obj = new Object();
obj.name = 'John'

// To access values, you can use dot notation or bracket notation. 
// Both do the same thing, bracket notion is useful for multispace keys,
// keys with dashes, or accessing values using variables
> obj.item1
> obj['item number 2']

> let b = 'item1'
  obj[b]
  // The following would NOT work and would return undefined:
  obj.b

// Checking exsistence of keys in object:
obj.toString ----- checks values of object and whatever object inherits from
> returns true

obj.hasOwnProperty('toString') ----- checks values of object only. Do this instead of checking like: (obj.name !== undefined)
> returns false


// Short hand key assignment:
const name = 'John'
const obj2 = {
    // this is short hand that automatically sets the key to be 'name',
    // and it's value to be 'John'
	name, 
}

// Constructor objects:
function Obj(name, age){
	this.name = name
  	this.age = age
}

const person = new Obj('john', 1)

// adding functions, couple ways:
const obj = {
  	name: 'Mike',
  	number: 4421,
	sayHi: () => console.log('hi'),
  	sayBye() {
    	console.log('say bye')
    },
    // getter function to get values from object. Has different usecases, but the same as doing obj.number
  	get getNumber(){
    	return this.number
    }
    // setter function to set values in object. Has different usecases, but the same as doing obj.number = 152
    set setNumber(num){
    	this.number = num
    }
}

obj.sayHi()
obj.sayBye()
obj.getNumber //Note how it's being accessed like a standard property, and not a function
obj.setNumber //Note how it's being accessed like a standard property, and not a function
QuietHumility

objetos javascript

const someObj = {
  propName: "John"
};

function propPrefix(str) {
  const s = "prop";
  return s + str;
}

const someProp = propPrefix("Name");
console.log(someObj[someProp]);
Real Ratel

Como criar um objeto em JavaScript

const person = {
  name: 'Anthony',
  age: 32,
  city: 'Los Angeles',
  occupation: 'Software Developer',
  skills: ['React','JavaScript','HTML','CSS']
}

//Use Template Literal to also log a message to the console
const message = `Hi, I'm ${person.name}. I am ${person.age} years old. I live in ${person.city}. I am a ${person.occupation}.`;
console.log(message);
Anthony Smith

Respostas semelhantes a “objeto em javascript”

Perguntas semelhantes a “objeto em javascript”

Mais respostas relacionadas para “objeto em javascript” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código