【问题标题】:Solidifying knowledge of javascript objects, methods, and properties巩固 javascript 对象、方法和属性的知识
【发布时间】:2016-07-18 05:04:51
【问题描述】:

我只是想巩固(肯定知道)什么是 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 object methods properties


    【解决方案1】:

    事实上,在 Javascript 中,对象字面量是零对或多对属性名称和对象关联值的列表,用花括号 ({}) 括起来。

    您可以查看The MDN Specification for Object Literals 了解更多信息。

    如果你写的话:

    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 中的函数。

    【讨论】:

    • 我很困惑的另一件事需要让开,方法命令主要是内置的还是有自定义方法(不仅仅是标识符)?
    • 抱歉,我不明白?你这是什么意思?
    • 你的意思是像hasOwnProperty()propertyIsEnumerable()这样的文档中的方法 ....?
    • 它将是window.i();或直接使用i();
    • 是的,事实上,在 javascript 中,当您在全局范围内(而不是在任何对象或函数中)声明一个函数时,它将保存在 window 对象中,因为它是全局范围。
    【解决方案2】:

    这是一个变量

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

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2020-11-16
      相关资源
      最近更新 更多