繁体   English   中英

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

[英]Solidifying knowledge of javascript objects, methods, and properties

我只想巩固(肯定知道)什么是 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()您只是在调用 Object Cachie的属性objecto中的函数hold。

这是一个变量

var property

这是一个函数声明

function method() { };

这是一个 object 类型的变量(以 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() methodobj的成员, obj是一个对象,该成员的类型是function 您可以通过调用typeof(obj.method)来测试它,它应该返回'function'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM