javascript - 带方法的 JSON 对象的正确术语是什么?

标签 javascript json

如果我有以下代码:

var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
       return this.firstName + " " + this.lastName;
    }
};

它叫什么?即使它具有函数绑定(bind),它是否只是一个 JSON 对象?在我给出的示例中,我可以调用 fullName 方法吗?

最佳答案

您可以发现 JSON 和对象字面量 here 的区别.

  • person is an object literal.

  • Properties (firstName, lastName, id) is like a noun which refers the person details.

  • Method (fullName) is like a verb that describes an action.

var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
       return this.firstName + " " + this.lastName;
    }
};

console.log("FirstName :", person.firstName);
console.log("Full Name :", person.fullName());

在这里,人是一个对象。它有一个属性 person.firstNameperson.lastNameperson.id 和方法 person.fullName() 返回人员的全名,其中包括人员 firstNamelastName 属性。

关于javascript - 带方法的 JSON 对象的正确术语是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099052/

相关文章:

javascript - $.getJSON 函数与 json 对象

python - Tornado :获取请求参数

javascript - 文本框属性更改不起作用 jQuery

javascript - 裁剪响应式全宽图像

javascript - 无法使用 react 和 href 元素在本地主机中下载 pdf 文件

javascript - 使用搜索关键字对数组进行排序 - Javascript

javascript - iframe 的自动滚动对我不起作用

javascript - Paper JS 的编程点击事件

java - 将 JSON 发布到 Spring MVC Controller 返回 400 错误请求

jquery - 从数组中删除特定对象并显示表中剩余的内容