小编Gra*_*ham的帖子

为什么JavaScript中的子对象失去了全局范围?

我正在努力遵循Douglas Crockford的建议,来自"JavaScript:The Good Parts"和他的网站:

应尽量减少全局变量的使用.绝不应使用隐含的全局变量.

为此,我定义了一个"根"对象,它充当所有其他对象的容器,现在所有内容都被安排到共享功能的逻辑层次结构中.

我难过的地方是子对象似乎失去了全局对象的范围.我能想到的最好的例子是我的记录器,我想将其全局定义为root.log并在其他地方重用它.

但是,当我尝试访问子对象内的root.log时,我的代码失败了,因为它无法再看到对对象的任何引用.我将子对象移动到全局范围内,它再次看到一切正常.

我已经看到Stack Overflow上的其他帖子通过将父引用显式传递给子进程来为父/子对象通信提供解决方案,但这并不是我在这之后所做的.我希望能够从任何一点访问root,如果我是三级或四级,我不想处理跟踪链.

一个明确的例子可能是,如果我在我的实用程序层次结构深处,我想记录一条消息.假设我在root.util.array.getPositionInArray()并且我将父值传递给每个子对象.我不想调用parent.parent.parent.log.write,我只想对root.log.write进行一次简单的调用.

我可以在创建它们时将root和父对象引用传递给每个子对象,或者可以尝试一些继承原则,看看我是否可以通过这种方式使用它.

我的问题如下:

  1. 当我在另一个对象中定义的对象时,为什么全局范围"消失"?

  2. 是否有一种简单的方法可以从子对象内部访问该全局变量?

  3. (也许是2的重复)推荐的处理方法是什么?

我的示例代码如下(这里加载到jsfiddle)

// declare root object as global variable
var root = null;

$(document).ready(function() {

    // instantiate root
    root = new Foo();

    // uncomment to instantiate child separately
    // child = new ChildFoo();

    // write to log from outside parent (shows scope is global)
    root.log.write(root.x)
    root.log.write(root.child.x);


});

function Foo() {

    // …
Run Code Online (Sandbox Code Playgroud)

javascript oop

0
推荐指数
1
解决办法
585
查看次数

如何使用 HTML5 画布标签显示视频

我想使用画布显示视频,包括播放暂停功能,允许用户通过单击画布来切换播放,我还希望在视频暂停时在视频顶部绘制叠加层。这是如何在 javascript 中完成的?

画布可用于显示来自各种来源的视频。此示例展示了如何将视频加载为文件资源、显示它并在屏幕播放/暂停切换上添加一个简单的点击。

只是一个图像

就画布而言,视频只是一个图像。您可以像绘制任何图像一样绘制它。不同之处在于视频可以播放并且有声音。

获取画布和基本设置

// It is assumed you know how to add a canvas and correctly size it.
var canvas = document.getElementById("myCanvas"); // get the canvas from the page
var ctx = canvas.getContext("2d");
var videoContainer; // object to hold video and associated info
Run Code Online (Sandbox Code Playgroud)

创建和加载视频

var video = document.createElement("video"); // create a video element
video.src = "urlOffVideo.webm"; 
// the video will now begin to load.
// As some additional info is needed we will place the video …
Run Code Online (Sandbox Code Playgroud)

javascript canvas html5-video html5-canvas

0
推荐指数
1
解决办法
4914
查看次数

选择角度2中的所有复选框?

如果用户选择角度为"所有邻域"复选框,则必须选中所有复选框.我试过下面的代码.但不行.如何在角度2中解决这个问题?

探索-list.Component.html

    <div class="checkbox_cont">
        <div class="checkbox ">
            <!--<input id="modal_checkbox1" type="checkbox" name="categories" unchecked="">-->
            <input type="checkbox" name="allneighbourhoods"  [value]="allneighbourhoods" (change)="neighbourhoodname [$event.target.getAttribute('value')]=$event.target.checked" id="allneighbourhoods" ng-click="toggleSelect($event)" />
            <label for="allneighbourhoods">All Neighbourhoods</label>
        </div>
    </div>
    <div class="modal_line"></div>                                                  
    <div class="checkbox " *ngFor="let neighbourhood of neighbourhoods;let i=index;">
        <input type="checkbox" name="neighbourhoodname[{{i}}]"  [value]="neighbourhood" (change)="neighbourhoodname [$event.target.getAttribute('value')]=$event.target.checked" id="{{neighbourhood}}"  [checked]='true' />
        <label for="{{neighbourhood}}">{{neighbourhood}}</label>
    </div> 
Run Code Online (Sandbox Code Playgroud)

探索-list.Component.ts

    export class ExploreListComponent implements OnInit {  
        neighbourhoodname={};

        toggleSelect = function(event){       
            this.forEach(this.checkboxes, function(item){
                console.log(item);
                item.selected = event.target.checked;
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

邻居json

在此输入图像描述

angular

0
推荐指数
1
解决办法
8131
查看次数

无效的配置对象:webpack.server.js

我正在使用这个 webpack 文件,但出现错误。

这是 webpack 文件:

const path = require('path');
module.exports = {
target: 'node',
entry: './src/index.js',
output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build')
},
module: {
    rules: [
        {
            test: /\.js?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            options: {
                presets: [
                    'react',
                    'stage-0',
                    ['env', { targets: { browsers: ['last 2 versions']}}]
                ]
            }
        }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

Invalid configuration object. Webpack has been initialised using a 
    configuration object that does not match the API schema.
    - configuration.context: The provided value …
Run Code Online (Sandbox Code Playgroud)

webpack-dev-server

0
推荐指数
1
解决办法
1394
查看次数

如何将图像中所有文本的强度(暗度)提高到一个级别?

我使用 Pytesseract 和 openCV 从图像中读取文本。我使用中值模糊、归一化和阈值来去除背景并能够阅读文本。

但是,在标准化过程中,文本的某些部分变得太亮,我希望将它们变暗,以便它们与图像中剩余文本的暗度/强度相匹配。我尝试了形态变换,并尝试了 canny+erosion 来消除噪音,但这些都没有帮助。

我的输入如下所示:

输入

在这里,“代码”,“部门名称”,“15”和“机械”较轻,我无法阅读,而我可以轻松阅读“空气分配”和“基本材料和方法”。

有关如何更改较浅文本颜色的任何帮助都会非常有帮助。

ocr opencv python-3.x spyder python-tesseract

0
推荐指数
1
解决办法
1964
查看次数