小编Mat*_*ood的帖子

在类中的函数之前,"get"关键字是什么?

get这个ES6课程意味着什么?我该如何参考这个功能?我该怎么用?

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get area() {
    return this.calcArea()
  }

  calcArea() {
    return this.height * this.width;
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript methods getter

86
推荐指数
4
解决办法
3万
查看次数

ES6中的地图与对象,何时使用?

参考:MDN地图

当密钥未知时直到运行时,并且当所有键都是相同类型且所有值都是相同类型时,请使用对象上的映射.

当存在对各个元素进行操作的逻辑时使用对象.

题:

在对象上使用地图的适用示例是什么?特别是"什么时候直到运行时才能知道密钥?"

var myMap = new Map();

var keyObj = {},
    keyFunc = function () { return 'hey'},
    keyString = "a string";

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

console.log(myMap.get(keyFunc));
Run Code Online (Sandbox Code Playgroud)

javascript javascript-objects ecmascript-6

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

如何使用Go提供JSON响应?

问:目前我打印出我的回应func Index 是这样fmt.Fprintf(w, string(response)) 但是,我怎么能在请求发送正确的JSON,以便它可能由视图消耗?

package main

import (
    "fmt"
    "github.com/julienschmidt/httprouter"
    "net/http"
    "log"
    "encoding/json"
)

type Payload struct {
    Stuff Data
}
type Data struct {
    Fruit Fruits
    Veggies Vegetables
}
type Fruits map[string]int
type Vegetables map[string]int


func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    response, err := getJsonResponse();
    if err != nil {
        panic(err)
    }
    fmt.Fprintf(w, string(response))
}


func main() {
    router := httprouter.New()
    router.GET("/", Index)
    log.Fatal(http.ListenAndServe(":8080", router))
}

func getJsonResponse()([]byte, error) {
    fruits := make(map[string]int) …
Run Code Online (Sandbox Code Playgroud)

json go

66
推荐指数
3
解决办法
8万
查看次数

动画画布看起来像电视噪音

我有一个名为的函数generateNoise(),它创建一个canvas元素并为其绘制随机的RGBA值; 这,给出了噪音的外观.


我的问题

什么是无限制动噪音的最佳方式,以给出运动的外观.这可能会有更多的生命?


的jsfiddle

function generateNoise(opacity) {
    if(!!!document.createElement('canvas').getContext) {
        return false;
    }
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d'),
        x,y,
        r,g,b,
        opacity = opacity || .2;

        canvas.width = 55;
        canvas.height = 55;

        for (x = 0; x < canvas.width; x++){
            for (y = 0; y < canvas.height; y++){
                r = Math.floor(Math.random() * 255);
                g = Math.floor(Math.random() * 255);
                b = Math.floor(Math.random() * 255);

                ctx.fillStyle = 'rgba(' + r + ',' + b + ',' + g + …
Run Code Online (Sandbox Code Playgroud)

javascript html5 canvas image-processing html5-canvas

60
推荐指数
6
解决办法
2万
查看次数

使用Node解析XLSX并创建json

好的,所以我发现这个记录非常好,node_module名为js-xlsx

问题: 如何解析xlsx以输出json

以下是Excel工作表的样子:

在此输入图像描述

最后,json应如下所示:

[
   {
   "id": 1,
   "Headline": "Team: Sally Pearson",
   "Location": "Austrailia",
   "BodyText": "...",
   "Media: "..."
   },
   {
   "id": 2,
   "Headline": "Team: Rebeca Andrade",
   "Location": "Brazil",
   "BodyText": "...",
   "Media: "..."
   }
]
Run Code Online (Sandbox Code Playgroud)

index.js:

if(typeof require !== 'undefined') {
    console.log('hey');
    XLSX = require('xlsx');
}
var workbook = XLSX.readFile('./assets/visa.xlsx');
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function(y) { /* iterate through sheets */
  var worksheet = workbook.Sheets[y];
  for (z in worksheet) {
    /* all keys that do …
Run Code Online (Sandbox Code Playgroud)

javascript excel json xlsx node.js

43
推荐指数
4
解决办法
7万
查看次数

如何从ui-router statechange返回$ state.current.name

.state('name')当我改变角度的位置时,我想返回 .

从我run()可以返回$state对象:

 .run(function($rootScope, Analytics, $location, $stateParams, $state) {
      console.log($state);
Run Code Online (Sandbox Code Playgroud)

工作对象

但是当我试图得到$state.current它是空物

.run(function($rootScope, $location, $stateParams, $state) {

      console.log($state.current);
Run Code Online (Sandbox Code Playgroud)

空

配置示例:

 .config(function($stateProvider, $urlRouterProvider, AnalyticsProvider) {  
        $urlRouterProvider.otherwise('/');
        $stateProvider
        .state('home', {
            url: '/',
            views: {
              '': {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
              },
              'navigation@home': {
                templateUrl: 'views/partials/navigation.html',
                controller: 'NavigationCtrl'
              },
              'weekly@home': {
                templateUrl: 'views/partials/weekly.html',
                controller: 'WeeklyCtrl'
              },
              'sidepanel@home': {
                templateUrl: 'views/partials/side-panel.html',
                controller: 'SidePanelCtrl'
              },
              'shoppanel@home': {
                templateUrl: 'views/partials/shop-panel.html',
                controller: 'ShopPanelCtrl'
              },
              'footer@home': {
                templateUrl: 'views/partials/footer.html',
                controller: 'FooterCtrl'
              }
            } …
Run Code Online (Sandbox Code Playgroud)

javascript routes angularjs angular-ui-router

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

可以在es6中写一个gulp文件吗?

问题:如何在ES6中编写gulp文件,以便我可以使用import而不是require使用=>语法function()

我可以使用io.js或节点任何版本.


gulpfile.js:

import gulp from "./node_modules/gulp/index.js";
gulp.task('hello-world', =>{
    console.log('hello world');
});
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

错误:

import gulp from "./node_modules/gulp/index.js";
^^^^^^
SyntaxError: Unexpected reserved word
Run Code Online (Sandbox Code Playgroud)
gulp.task('hello-world', =>{
                         ^^
SyntaxError: Unexpected token =>
Run Code Online (Sandbox Code Playgroud)

里面的node_modules/gulp/bin/gulp.js我已经改变了第一线#!/usr/bin/env node --harmony的要求在这个堆栈

javascript node.js ecmascript-6 gulp

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

在Angular 2中编写最基本的单元测试?

问题:只要我将Angular 2导入文件,就不会执行任何测试.

问题:如何设置我的业力配置以支持角度2,以便我的测试通过正确?

问题:我如何设置使用es6编写的angular2的任何测试框架?

Git Repo(确保你在分支角度-2上

噶:

// Karma configuration
// Generated on Mon Jun 01 2015 14:16:41 GMT-0700 (PDT)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jspm', 'jasmine'],


    // list of files / patterns to load in the browser
     jspm: {
        loadFiles: [
            'client/app/**/*.js'
        ]
    },


    // …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing ecmascript-6 karma-runner angular

28
推荐指数
1
解决办法
4431
查看次数

form_for在提交时如何知道差异:new:edit

我已经生成了一个脚手架,我们称之为脚手架测试.在那个脚手架中,我有一个_form.html.erb,它正在为动作渲染:new =>:create and:edit =>:update

Rails有时会做很多魔术,我无法弄清楚form_for如何知道如何调用正确的:在按下提交时动作:new和:edit

脚手架形式

<%= form_for(@test) do |f| %>


  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

与非脚手架形式

 <% form_for @test :url => {:action => "new"}, :method => "post" do |f| %>
       <%= f.submit %>
 <% end %>
Run Code Online (Sandbox Code Playgroud)

编辑模板

<h1>Editing test</h1>

<%= render 'form' %>
Run Code Online (Sandbox Code Playgroud)

新模板

<h1>New test</h1>

<%= render 'form' %>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,表单之间没有区别两个模板如何呈现相同的表单但使用不同的操作?

forms model-view-controller ruby-on-rails

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

错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块?

来自https://2ality.com/2019/04/nodejs-esm-impl.html Node 12 应该支持 es6 模块;但是,我只是不断收到错误消息:

问题:如何在节点 12 中使用 es6 模块制作 MVP?

包.json

{
  "name": "dynamic-es6-mod",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.mjs",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/index.mjs"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "globby": "^10.0.1"
  }
}
Run Code Online (Sandbox Code Playgroud)
$ node -v
$ 12.6.0
$ npm run start


internal/modules/cjs/loader.js:821
  throw new ERR_REQUIRE_ESM(filename);
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/dev/dynamic-es6-mod/src/index.mjs
    at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:821:9)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at …
Run Code Online (Sandbox Code Playgroud)

javascript import node.js es6-modules

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