小编Ant*_*tiz的帖子

最后一段javascript闭包我不明白

让我们说我们已经function在全球范围内定义了这个:

function createCounter() {
  var counter = 0;

  function increment() {
    counter = counter + 1;

    console.log("Number of events: " + counter);
  }

  return increment;
}
Run Code Online (Sandbox Code Playgroud)

在解释闭包的大多数示例中,我看到执行:

createCounter();
Run Code Online (Sandbox Code Playgroud)

从全局范围将只返回内部函数:

function increment() {
    counter = counter + 1;

    console.log("Number of events: " + counter);
}
Run Code Online (Sandbox Code Playgroud)

现在这完全有道理,因为createCounter函数声明中的这一行

return increment;
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,为什么这样:

var counter1 = createCounter();

counter1();

Number of events: 1 //result
Run Code Online (Sandbox Code Playgroud)

最后得到的功能是什么?

本质上不是counter1createCounter指向全局范围内的内部函数的指针?

也许更好的方式来问这是为什么counter1()工作而不仅仅是返回内部函数createCounter呢?

javascript

5
推荐指数
1
解决办法
64
查看次数

Express 中虚拟路径前缀的用途

在 Express 中,在服务器端提供静态服务的方式似乎非常简单:

要提供静态文件(例如图像、CSS 文件和 JavaScript 文件),请使用 Express 中的express.static 内置中间件函数。

将包含静态资产的目录名称传递给express.static中间件函数以直接开始提供文件服务。例如,使用以下代码在名为 public 的目录中提供图像、CSS 文件和 JavaScript 文件:

app.use(express.static('public'))
Run Code Online (Sandbox Code Playgroud)

现在,您可以加载公共目录中的文件:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
Run Code Online (Sandbox Code Playgroud)

Express 查找相对于静态目录的文件,因此静态目录的名称不是 URL 的一部分。

要使用多个静态资产目录,请多次调用express.static中间件函数:

app.use(express.static('public'))
app.use(express.static('files'))
Run Code Online (Sandbox Code Playgroud)

Express 按照您使用express.static 中间件函数设置静态目录的顺序查找文件。

我明白了虚拟路径前缀的想法,但为什么要使用它呢?

要为express.static函数提供的文件创建虚拟路径前缀(该路径实际上并不存在于文件系统中),请指定静态目录的安装路径,如下所示:

app.use('/static', express.static('public'))
Run Code Online (Sandbox Code Playgroud)

现在,您可以从 /static 路径前缀加载 public 目录中的文件。

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
Run Code Online (Sandbox Code Playgroud)

express

5
推荐指数
1
解决办法
2210
查看次数

Next.js:实现私有路由时,如何防止在重定向之前出现未经授权的路由/页面?

profile本质上,我为 Next.js 应用程序中的两个页面(即和)创建了一个 HOC,dashboard两个页面阻止用户在未经授权的情况下访问它们。

例子:pages/profile.js

import withAuth from "../components/AuthCheck/index";

function Profile() {
  return (
    <>
      <h1>Profile</h1>
    </>
  )
}


export default withAuth(Profile);
Run Code Online (Sandbox Code Playgroud)

我的身份验证组件/HOC:

import { useRouter } from 'next/router'
import { useUser } from '../../lib/hooks'
import { PageLoader } from '../Loader/index'

const withAuth = Component => {
  const Auth = (props) => {
    const { isError } = useUser(); //My hook which is calling /api/user see if there is a user
    const router = useRouter()


    if …
Run Code Online (Sandbox Code Playgroud)

reactjs higher-order-components next.js next-router

5
推荐指数
1
解决办法
1万
查看次数

有人可以解释你在这个函数表达式中如何得到错误吗?

我理解了第一部分if,但不是第二部分说明"因为'n'不等于零,在用逻辑NOT(!)运算符修改的偶函数中返回'n'."?如果我作为参数传递5,那么不会返回4 fn();吗?

var fn = function even (n)
{
    if (n === 0)
    {
        return true;
    }
    else 
    {
        return !even(n - 1)
    }    
};

fn(5); //=> false
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
2
解决办法
98
查看次数

为什么这会返回3,1?

我理解后者,我们在foo对象上调用警报,该对象具有另一个名为baz其属性的对象,该对象又有一个名为barreturn的方法返回值x.而且因为lexical scope(我认为:))的JS编译器/解释上升链,发现xbaz返回1.

我的猜测是,当分配给变量go然后从全局范围调用时,你得到3?只是想知道背景中发生了什么.任何帮助将不胜感激!!!

var x = 3;

var foo = {
    x: 2,
    baz: {
        x: 1,
        bar: function() {
            return this.x;
        }
    }
}

var go = foo.baz.bar;

alert(go());
alert(foo.baz.bar());
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
1
解决办法
876
查看次数

为什么这个表达式在javaScript中返回2?

我认为你可能会得到0,也许是因为字符串被转向1's并且-操作符导致减法操作?

"1" - - "1";
Run Code Online (Sandbox Code Playgroud)

提前致谢!

javascript

4
推荐指数
1
解决办法
92
查看次数

未捕获的TypeError:无法在JavaScript函数中读取null(...)的属性"1"

该方法是模块的一部分; 尽管有错误......

Uncaught TypeError: Cannot read property '1' of null(…)

在很小程度上工作,但它似乎阻止了模块上的其他方法.

这是一个包含整个模块的小提琴.

searchURL: function() {
  function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  }
  var link, url, parser, newPathName = '',
    newstr = '',
    doc = document,
    avon_rep_container = doc.getElementById('avon_rep_container'),
    avon_rep_container_id,
    avon_rep_container_links,
    avon_rep_container_images,
    documentTableWrapper,
    docBodyFirstChild,
    full_width_container_1 = doc.getElementsByClassName('full-width-container')[1],
    full_width_img_class_el = doc.getElementsByClassName('full-width-img')[0];
  if (!avon_rep_container) {
    avon_rep_container = doc.createElement('div');
    avon_rep_container.setAttribute('id', 'avon_rep_container');
    avon_rep_container.className = 'container-avon-representative-news';
    avon_rep_container_links = avon_rep_container.getElementsByTagName('a');
    avon_rep_container_id = doc.getElementById('avon_rep_container');
    docBodyFirstChild = doc.body.firstChild;
    documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
    avon_rep_container.appendChild(documentTableWrapper);
    doc.body.appendChild(avon_rep_container);
    full_width_container_1.removeChild(full_width_container_1.getElementsByTagName('table')[0]);
    full_width_img_class_el.removeAttribute('style');
  } else …
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
1
解决办法
2万
查看次数

为什么Node.js是用C/C++编程语言编写的?

不幸的是,JavaScript是我遇到的唯一编程语言.所以我的直觉很自然地想知道为什么你不会在JavaScript中使用编写一种编程语言(在本例中为Node)?

为何选择C?你得到了什么好处?

node.js

4
推荐指数
1
解决办法
5668
查看次数

了解如何在香草JavaScript中实现lodash的_.flowRight

在学校里,我们的任务是构建lodash方法flowRight的实现!

在规格中提到:

接受任意数量的函数,并返回一个使用其参数的新函数,并从右到左(最后到第一个)调用提供的函数。每个函数(第一个函数除外)的参数由函数右侧的返回值确定。由flowRight返回的函数的调用求值为最左边的函数的返回值。

这是他们提供的示例:

e.g.

var sayHello = function (name) {
    return 'Hello, ' + name;
},

addExclamation = function (s) {
    return s + '!';
},

smallTalk = function (s) {
    return s + ' Nice weather we are having, eh?';
};

var greetEnthusiastically = flowRight(addExclamation, sayHello);

greetEnthusiastically('Antonio');
// --> returns 'Hello, Antonio!'
//(sayHello is called with 'Antonio', 
//  addExclamation is called with 'Hello, Antonio')
Run Code Online (Sandbox Code Playgroud)

我感觉像我了解一个静态示例(该示例演示)中发生的事情。

function (func1, func2) {
    return function(value) {
        return func1(func2(value));
    }
}
Run Code Online (Sandbox Code Playgroud)

猜猜我很难将自己的大脑环绕在循环中,我认为这是您所需要的。到目前为止,这是我的实现。 …

javascript recursion closures lodash

4
推荐指数
2
解决办法
792
查看次数

如何在Chart.js中将Y轴值从数字更改为字符串?

我正在使用Chart.js,并试图更改y轴(请参见下面的屏幕截图)。我尝试yLabels用字符串数组填充属性。但这没有用。任何帮助,将不胜感激!

我的条形图

 jQuery(document).ready(function($) {
    'use strict ';
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["HTML", "CSS", "SCSS", "JavaScript"],
            yLabels: [
                'newb',
                'codecademy',
                'code-school',
                'bootcamp',
                'junior-dev',
                'mid-level',
                'senior-dev',
                'full-stack-dev',
                'famous-speaker',
                'unicorn'
            ],
            datasets: [{
                data: [12, 19, 3, 10],
                backgroundColor: [
                    'rgba(255, 159, 64, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(255, 206, 86, 0.2)'

                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, …
Run Code Online (Sandbox Code Playgroud)

javascript chart.js

2
推荐指数
1
解决办法
2591
查看次数