【问题标题】:How can I write a javascript function in ES6如何在 ES6 中编写 javascript 函数
【发布时间】:2018-11-21 15:53:13
【问题描述】:

我一直在创建一个 Web 应用程序,并使用两个不同的 DIV 将注册页面和登录页面组合在同一个 HTML 文件中。当页面加载时,“登录 Div”是唯一可见的,当您单击在使用一些 JavaScript 注册“Register Div is Shown”。所以它工作正常,但我想在 ES6 中编写这些函数。我该怎么做?

function Registration()
{
        const element = document.querySelector(".Registration-form"); // to give a class to the button
        const element2=document.querySelector(".login-form");
        var unhide=element.style.display="block";
        var hider=element2.style.display="none";
}
function login()
{
        const element = document.querySelector(".login-form"); // to give a class to the button
        const element2 = document.querySelector(".Registration-form"); // to give a class to the button
        var unhide=element.style.display="block";
        var hider=element2.style.display="none";
}

【问题讨论】:

  • “我想在 ES6 中编写这些函数”到底是什么意思?
  • 你并没有真正在 ES6 中编写代码。归根结底,它只是 JavaScript。 ES6 为语言添加了额外的特性等。你具体想做什么?
  • 这些函数已经完全兼容 ES6,所以不知道你在期待什么。如果需要 const registration = () => { ... },您可以将 function(){ ... } 定义更改为箭头,但这对于显示的代码没有任何作用。旁注:无需将样式更改分配给变量。

标签: javascript ecmascript-6


【解决方案1】:

通过谷歌搜索我很确定你可以看到,但这里是:

const registration =() => {

        const element = document.querySelector(".Registration-form");
        const element2=document.querySelector(".login-form");

        const unhide=element.style.display="block";
        const  hider=element2.style.display="none";
}
const  login = () => {
        const element = document.querySelector(".login-form"); // to give a class to the button
        const element2 = document.querySelector(".Registration-form"); // to give a class to the button
        const unhide=element.style.display="block";
        const hider=element2.style.display="none";
}

这些类型的函数称为箭头/粗箭头函数。您应该阅读它们,因为在某些情况下它们不能用作意图。

【讨论】:

  • 我认为您的意思是“按预期”,箭头功能完全按预期工作:)
  • @James 是的,是的!上帝,我永远学不会,抱歉,英语是我的第二语言! :>
  • 这里没有理由使用箭头函数。
  • 这就是 OP 想要的,如何编写 ES6 函数。
  • 那些不是 ES6 函数。它们是箭头函数。没有像 ES6 函数这样的东西。
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2014-08-02
相关资源
最近更新 更多