JavaScript review object

Time:2022-5-3

JavaScript review object
Create an object with the new keyword:

var obj = new object();
obj.uname = 'hlr';
obj.age = 18;
obj. Sex = 'female';
obj.sayHi = function(){
	console.log(Hi~);
}
//Call object
console.log(obj.uname);
console.log(obj['age']);
obj.sayHi();

Create objects using constructors:

function Fruit(name,price){
				this.name = name;
				this.price = price;
				this.description = function(y){
					console.log(y);
				}
				
			}
			
			var peach = new Fruit('peach',10.00);
			peach.description('yummy');
			console.log(peach.price);

For in traversal object:

var obj = {
				myname:'vivi',
				age:18,
				Sex: 'female',
			}
			for(var k in obj){
				console. log(obj[k]); // The result is the value of the attribute
				console. log(k); // The result is the property name
			}