巩固javascript对象、方法和属性的知识

Solidifying knowledge of javascript objects, methods, and properties

本文关键字:属性 知识 方法 javascript 对象 巩固      更新时间:2023-09-26

我只想巩固(确切地知道)什么是js对象、方法和属性。到目前为止,这是我自己的观点,但我几乎没有怀疑,这就是为什么我在这里知道并证明我需要的是真实的。

var property= "is this a property?";
function method(){"is this a method?"};
var ObjectLiteral = {property:"this should be a property of this ObjectLiteral I guess", method:function(){"this should be a method of ObjectLiteral am I right?"}};
//Here is the cache:
var Cachie = {method : function SecondObject(){/*nothing in here just a regular function or object*/},objecto:function(){alert("is this a method or object of Cachie object if so methods and properties are functions and variables")}};
Cachie.objecto();//than what is objecto related to Cachie and what is objecto called related Cachie is it an object of Cachie by the way, or is just simply called an object and nothing else of Cachie whatsoever?

事实上,在Javascript中,对象文字是一个对象的零对或多对属性名称和相关值的列表,用大括号({})括起来。

有关更多信息,您可以查看对象文字的MDN规范

在你的情况下,如果你写:

var Cachie = {method : function SecondObject(){/*nothing in here just a regular function or object*/},objecto:function(){alert("is this a method or object of Cachie object if so methods and properties are functions and variables")}};
Cachie.objecto();

意味着:

您有一个名为Cachie的文字对象,它有两个属性:methodobjecto,它们是这里的函数,所以为了回答您的问题objecto与Cachie有什么关系objectoCachie对象的函数和属性。

因此,当您调用Cachie.objecto()时,您只是在对象Cachie的属性objecto中调用函数hold。

这是一个变量

var property

这是一个函数声明

function method() { };

这是一个对象类型的变量(以JSON格式声明),有2个属性。

var obj = {
    property: "", 
    method: function() {}
};

同样的东西可以这样写,当然不是JSON:

var obj = new Object();
obj.property = "";
obj.method = function() {};

var obj = new function() {
    this.property = "";
    this.method = function() {};
}

等等,还有其他方法。

您的其他示例与上面的示例相同,例如,调用obj.method()method是作为对象的obj的成员,并且该成员的类型为function。您可以通过调用typeof(obj.method)对此进行测试,它应该返回'function'