我正在构建一个使用var创建'level'对象的javascript游戏:
function start() {
var myGameLevel = new Level(2);
}
Run Code Online (Sandbox Code Playgroud)
这个"Level()"对象具有很多功能,主要是向DOM添加元素并使它们具有交互性.简化:
function Level(i) {
var _difficulty = i;
this.init = function(){
jQuery("#container").append(...game elements here...);
jQuery("#button").on('click', function() {...});
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:如何知道在'start'函数中创建的Level对象是否已被垃圾回收?我的目标是只使用"var"变量,以便没有外部引用.当DOM被清除所有游戏元素时,我预计"级别"对象将从内存中释放,但我怎么能确定?
我正在阅读Vue组件,并找到他们为什么数据需要成为一个功能有点令人困惑的解释:
根实例
var vm = new Vue({
el: '#example',
data: {
message: 'here data is a property'
}
})
Run Code Online (Sandbox Code Playgroud)
一个组件
var vm = new Vue({
el: '#example',
data: function () {
return {
counter: 0
}
}
})
Run Code Online (Sandbox Code Playgroud)
Vue文档通过为每个组件分配一个全局计数器变量来解释这种差异,然后他们惊讶地发现每个组件共享相同的数据......他们也没有解释为什么他们已经在这里使用了一个函数.
var data = { counter: 0 }
Vue.component('simple-counter', {
template: '<div>{{ counter }}</div >',
data: function () {
return data
}
})
Run Code Online (Sandbox Code Playgroud)
当然,数据现在是共享的
<simple-counter></simple-counter>
<simple-counter></simple-counter>
<simple-counter></simple-counter>
Run Code Online (Sandbox Code Playgroud)
当您引用全局对象作为数据源时,组件没有自己的数据就不足为奇了.对于将数据作为属性的根Vue实例也是如此.
var mydata = { counter: 0 }
var vm1 = new Vue({ …Run Code Online (Sandbox Code Playgroud) 我有一个基本的 HTML Canvas,我正在其中绘制一些图像。然后我想将画布保存为 PNG 文件。我在 stackoverflow 上找到了答案:toDataURL() 将画布数据发送到图像文件。但对我来说,这只适用于画布非常小的情况(例如,400x300 像素)。如果画布为 1200x900 像素,则 toDataURL() 返回 0 字节的空 PNG 文件。我已经在本地主机和我的网络服务器上进行了测试。该图像与 html 页面来自同一域。
代码:
var data = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href = data;
Run Code Online (Sandbox Code Playgroud) 我在我的Typescript tsconfig.json文件中添加了“ noImplicitAny”和“ noImplicitReturns”:
{
"compilerOptions": {
"target":"es5",
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedLocals":true,
"out": "dist/js/main.js"
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以下代码会产生错误或至少警告:
private randomMove() { // no return type but no warning :(
let o = 3; // no type for o but no warning :(
}
Run Code Online (Sandbox Code Playgroud)
“ noUnusedLocals”正在运行。
这是应该工作的方式吗,我是否缺少任何东西?当您不指定类型/返回类型时,是否可以使Visual Studio Code生成警告?
我正在按照Parcel的基本教程来捆绑 js、css 和图像文件。
我的文件结构
dist
node_modules
src
- index.html
- style.css
- index.js
- somemodule.js
Run Code Online (Sandbox Code Playgroud)
当我运行时,parcel ./src/index.html服务器启动并且网站正确显示。根据教程,您可以使用 来完成构建parcel build index.js。我期望得到这个输出:
dist
- style.css
- index.js
- index.html
Run Code Online (Sandbox Code Playgroud)
但是,运行后parcel build index.js我得到以下输出:
dist
- 4wyd98y98790.css
- 948y59hslas8.js
Run Code Online (Sandbox Code Playgroud)
可能出了什么问题?
我正在继续研究现有的打字稿项目.原始项目使用gulp-typescript编译.ts文件,但我使用的是Visual Studio Code附带的tsc编译器.但我不能让ES6承诺工作.
我已经为ES6承诺安装了一个定义文件:
npm install --save @types/es6-promise
Run Code Online (Sandbox Code Playgroud)
这将在node_modules文件夹中创建一个具有promise定义文件的目录:
node_modules
@types
es6-promise
index.d.ts
package.json
types-metadata.json
readme.md
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
"files": [
"src/main.ts",
"src/game.ts",
"node_modules/@types/es6-promise/index.d.ts"
]
Run Code Online (Sandbox Code Playgroud)
参考
///<reference path="../node_modules/@types/es6-promise/index.d.ts" />
Run Code Online (Sandbox Code Playgroud)
我假设包含promise定义文件现在正在工作,但在编译项目时出现以下错误:
error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
Run Code Online (Sandbox Code Playgroud)
当我查看原始代码时,我发现:
private componentsPromise: Promise<Component[]>;
private startPromise: Promise; // <- error
public getComponents(): Promise<Component[]> {
return this.componentsPromise;
}
public isStarted(): Promise { // <-- error
return this.startPromise;
}
Run Code Online (Sandbox Code Playgroud)
这是原始项目中的一个简单错误还是我错过了什么?我需要在这里添加什么才能让承诺再次发挥作用?
我正在使用 typescript 编译器将我的模块捆绑到一个 main.js 文件中,使用 tsconfig.json 中的这些设置:
"module": "system",
"out": "docs/js/main.js"
Run Code Online (Sandbox Code Playgroud)
这是有效的,所以根据SystemJS 文档,我只需要在我的 HTML 中包含 SystemJS 生产文件并使用这些标签启动应用程序:
<script src="js/system.js"></script>
<script>
SystemJS.import('js/main.js');
</script>
Run Code Online (Sandbox Code Playgroud)
我的应用程序:
import { Message } from "./message";
export class App {
constructor() {
let demomessage = new Message("hello");
}
}
export class Message {
constructor(str:string) {
console.log(str);
}
}
Run Code Online (Sandbox Code Playgroud)
这会在 main.js 中生成以下 javascript 代码:
System.register("message", ...) {
// message code here
});
System.register("app", ...) {
// app code here
});
Run Code Online (Sandbox Code Playgroud)
我缺少的部分(微软总是缺乏的 Typescript-documentation也没有解释)是如何实际启动应用程序...... SystemJS 如何知道哪个类是起点?即使我只是将 …
我为 CSS 关键帧动画创建了一个非常基本的测试,包括“ontransitionend”的事件侦听器 - 但是无论我使用什么变体,这个“过渡结束”侦听器都不会触发。(我的完整代码具有所有供应商前缀)。
HTML
<div id="block">I like to move it</div>
Run Code Online (Sandbox Code Playgroud)
CSS
.tweetanimation {
-webkit-animation: Tweet_FadeInOut 6s 1 forwards;
}
@-webkit-keyframes Tweet_FadeInOut {
0% {
opacity:0;
left:30%;
}
30%, 70% {
opacity:1;
left:15%;
}
100% {
opacity: 0;
left:0%;
}
}
Run Code Online (Sandbox Code Playgroud)
爪哇脚本
$("#block").one('webkitTransitionEnd msTransitionEnd transitionend', function(e) {
alert("block has finished moving");
});
$("#block").addClass("tweetanimation");
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我无法让它工作!我错过了什么? JSFiddle在这里
我已经使用以下命令卸载了 npm 软件包“grunt-cli”和“tsd”:
\n\nsudo npm uninstall -g grunt-cli \nsudo npm uninstall -g tsd\nRun Code Online (Sandbox Code Playgroud)\n\n但现在,当我列出所有 npm 包时:
\n\nnpm -g ls --depth=0\nRun Code Online (Sandbox Code Playgroud)\n\n我收到这个错误。没有这样的文件或目录。这是正确的,因为我刚刚卸载了它们......
\n\n/usr/local/lib\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bower@1.7.1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 browserify@12.0.1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 express-generator@4.13.1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 error: ENOENT: no such file or directory, open '/usr/local/lib/node_modules/grunt-cli/package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 npm@3.5.3\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 error: ENOENT: no such file or directory, open '/usr/local/lib/node_modules/tsd/package.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 typescript@1.8.9\nRun Code Online (Sandbox Code Playgroud)\n\n为什么我会收到此错误?如何彻底删除grunt-cli和tsd?ENOENT 是什么意思? (尝试用谷歌搜索它......)
\n我正在使用Array.filter和instanceof从更通用的数组中获取某种类型的所有对象。
let cars:Car[] = this.gameObjects.filter(go => go instanceof Car)
Run Code Online (Sandbox Code Playgroud)
这有效!但是打字稿编译器不同意返回的类型都是 Car 类型。
类型“GameObject[]”不可分配给类型“Car[]”
在Typescript GitHub 上,这个问题被认为是通过为 array.filter 添加一个接口来解决的。这对我不起作用,它仍然给出相同的错误。您可以通过在打字稿游乐场中粘贴此代码来尝试一下:
interface Array<T> {
filter<U extends T>(pred: (a: T) => a is U): U[];
}
class Car {
drive() {
}
}
class GameObject {
}
let gameObjects:GameObject[] = [new Car(), new Car()]
let cars:Car[] = gameObjects.filter((go) => go instanceof Car)
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?如何按类型过滤数组并取回正确类型的数组?
javascript ×4
typescript ×4
arrays ×1
canvas ×1
css ×1
es6-promise ×1
generics ×1
interface ×1
jquery ×1
npm ×1
npm-install ×1
parceljs ×1
systemjs ×1
tsconfig ×1
vue.js ×1
vuejs2 ×1