Javascript内部函数与冒号 [英] Javascript inner function with colon

查看:124
本文介绍了Javascript内部函数与冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道内在的功能是什么样的?
我知道你可以写下面

I wonder what is the inner function like? I know that you can write following

var obj = {
       test: 'something'
}

但是在这段代码中,innerfunction不是指一个变量,而是一个函数。
有没有其他方法来编写/调用内部函数?

But in this code, the innerfunction does not refer to a variable, but to a function. Is there anyother way to write / call the inner function?

function outer(){

    var a = "Outerfunction";
    console.log(a)

    innerFct: function InnerFct()    {                                              
            var c = "Inner";
            console.log(c)
    }innerFct();
}
window.outer();

谢谢

推荐答案

这里有几件不同的事情。

There are a couple of different things going on here.

在此代码中:

var obj = {
    test: 'something'
}

你正在使用文字对象表示法来创建 - 好吧,一个属性 test 的对象,该属性的值为某事

you are using "literal object notation" to create -- well, an object with one property test and that property has a value of something

在第二种情况下,您正在创建一个代码块(是的,对象和代码块都使用它很有趣使用相同的语法 {...} 来定义它们。

In your second case, you are creating a code block (yes, it is fun that both objects and code blocks use the same syntax {...} to define them.

在代码块内部, innerFct:成为一个标签。标签与一些控制流语句一起使用来跳转。忘了它们,你真的最好不要使用它们。

Inside of a code block, the innerFct: becomes a label. Labels are used with some control flow statements to jump around. Forget about them, you really are better off not using them.

function outer(){
    var a = "Outerfunction";
    console.log(a)

    function innerFct()    {                                              
        var c = "Inner";
        console.log(c)
    }
    innerFct();
}
outer();

甚至

function outer(){
    var a = "Outerfunction";
    console.log(a)

    var innerFct = function ()    {                                              
        var c = "Inner";
        console.log(c)
    }
    innerFct();
}
outer();

这篇关于Javascript内部函数与冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆