import{_ as n,c as a,a as p,o as t}from"./app-DazB1p5v.js";const e={};function o(c,s){return t(),a("div",null,s[0]||(s[0]=[p(`

对象和类

对象

JavaScript中的对象(object)用于存储各种键值集合和更复杂的实体。例如下面的对象

var person = {
	firstName: 'John',
	lastName: 'Doe',
	age: 30,
	isStudent: false,
	greet: function() {
		console.log('Hello, I am ' + this.firstName + ' ' + this.lastName);
	}
	print_name() {
		console.log(lastName + ' ' + firstName) 
	}
};

如何使用对象

console.log(person.firstName);  // 输出: John
person.greet();  // 输出: Hello, I am John Doe

类

javascript中的类(class)是用于创建对象的模板,和c++中的类相似,但是需要在constructor()函数中声明类成员

class User {
	constructor(name) {
		this.name = name;
	}

	greet() {
		console.log("Hello, I'm " + this.name) 
	}
}

使用方法

var user = new User("Alice")
user.greet()
`,11)]))}const i=n(e,[["render",o],["__file","object_and_class.html.vue"]]),u=JSON.parse('{"path":"/note/language/javascript/object_and_class.html","title":"对象和类","lang":"zh-CN","frontmatter":{},"headers":[{"level":2,"title":"对象","slug":"对象","link":"#对象","children":[]},{"level":2,"title":"类","slug":"类","link":"#类","children":[]}],"git":{},"filePathRelative":"note/language/javascript/object_and_class.md"}');export{i as comp,u as data};