简体   繁体   English

setState:function(){是什么意思

[英]what does setState: function(){ mean

what does it mean when you set a function like this 设置这样的功能是什么意思

setState: function(){  
}

The code is incomplete, but it looks like you're assigning a function as the value to a property called setState of an object that isn't shown in your code. 代码不完整,但是看起来您正在将一个函数作为值分配给代码中未显示的对象的名为setState的属性。

An example: 一个例子:

var myObject = {
    prop1: 'abc',
    prop2: function() {
       alert('def');
    }
};

Above, I'm creating a variable called myObject that is an object with two properties, prop1 and prop2 . 上面,我正在创建一个名为myObject的变量,该变量是具有两个属性prop1prop2的对象。 The first one is a string. 第一个是字符串。 If I write alert(myObject.prop1) it'll alert "abc". 如果我编写alert(myObject.prop1)它将警告“ abc”。

The second one is a function. 第二个是功能。 If I write myObject.prop2() I'll execute that function, which'll alert "def". 如果我编写myObject.prop2()我将执行该函数,该函数将提示“ def”。

That would appear in the context of an object literal , like this: 这将出现在对象文字的上下文中,如下所示:

var obj = {
    setState: function(){  
    }
};

It assigns the function to the named property of the object. 它将功能分配给对象的命名属性。 The function is anonymous (it has no name), though the property has a name. 该函数是匿名的(它没有名称),尽管该属性有一个名称。 You could call the function like so: 您可以这样调用该函数:

obj.setState();

Literal notation is basically a series of propname: value within curly braces ( {} ), separated by commas, where value can be any valid value —a string, a number, a function reference... The property name can appear literally, as in your example, or in quotes ( "propname": value ) if the literal name is a reserved word (eg, you'd need quotes for a property called var since var is a reserved word). 文字符号基本上是一系列的propname: value在大括号( {} )中的value ,用逗号分隔,其中value可以是任何有效值—字符串,数字,函数引用...属性名称可以按字面出现,例如在您的示例中,如果文字名称是保留字,则用引号( "propname": value )(例如,由于var是保留字,因此需要对名为var的属性加引号)。

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

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