我知道在javascript中有两种方法可以确定变量是否存在而不是null(false,empty):
1) if ( typeof variableName !== 'undefined' && variableName )
2) if ( window.variableName )
哪一个更受欢迎,为什么?
我一直试图围绕Object.createECMAScript 5中引入的新方法.
通常,当我想使用继承时,我会这样做:
var Animal = function(name) { this.name = name; }
Animal.prototype.print = function() { console.log(this.name); }
var Dog = function()
{
return Animal.call(this, 'Dog');
}
Dog.prototype = new Animal();
Dog.prototype.bark = function() { console.log('bark'); }
Run Code Online (Sandbox Code Playgroud)
我只是将一个新创建的Animal对象分配给Dog的原型,一切都像魅力:
var dog1 = new Dog();
dog1.print(); // prints 'Dog'
dog1.bark(); // prints 'bark'
dog1.name; //prints 'Dog'
Run Code Online (Sandbox Code Playgroud)
但人们(没有解释)说这Dog.prototype = new Animal();不是继承的工作方式,我应该使用Object.create方法:
Dog.prototype = Object.create(Animal.prototype);
Run Code Online (Sandbox Code Playgroud)
这也有效.
使用的好处是什么,Object.create或者我错过了什么?
更新:有人说这Dog.prototype = Animal.prototype;也可以.所以现在我完全糊涂了
我试图了解如何在用GLSL编写的webgl着色器中模拟console.log.它很容易得到错误消息,但我无法得到如何打印自定义消息.
基本上我想在浏览器的控制台中打印东西:
<script id="shader-fs1" type="x-shader/x-fragment">
void main(void)
{
//console.log doesn't work here since it's GLSL not javascript
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我正在开发一个镀铬扩展,并遇到了一个大问题.
我正在使用内容脚本在网站上注入我的javascript代码.该网站有一个iframe.我可以更改iframe的源代码,但似乎无法访问iframe的contentWindow属性.我需要它在当前的carret位置插入文本.
所以基本上这个代码在页面的上下文中完美地工作:
$("#iframe1").contentWindow.document.execCommand("InsertHTML", false, 'test text');
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在我的chrome扩展程序的上下文中运行时,我收到此错误:
TypeError: Cannot read property 'document' of undefined
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我可以访问iframe的html.所以这段代码完全可以从chrome扩展程序中运行:
$("#iframe1").contents().find('div').html('test')
Run Code Online (Sandbox Code Playgroud)
我尝试在清单文件中放入"all_frames":true但没有运气:(
我被问到一个很好的编程问题:
在输入中,我有0到255(1字节)的100个唯一数字.我一次只能读一个号码.我有40个字节的内存,我可以使用.目标是对所有数字进行排序并在输出中打印它们.我确信数字的唯一性非常重要.
有任何想法吗?
我目前正在使用附加SDK开发Firefox扩展,并遇到了一个真正的问题.基本上我的扩展只是将内容脚本注入到这样的网页中:
main.js
var pageMod = require("page-mod");
var self = require("self");
pageMod.PageMod({
include: "http://mail.google.com/mail/*",
contentScriptFile: [self.data.url("jquery.js"),
self.data.url("start.js")],
attachTo : ["top"]
});
Run Code Online (Sandbox Code Playgroud)
start.js
$('body').append('<div>1</div><img src="insertnote.png" /><div>2</div>');
Run Code Online (Sandbox Code Playgroud)
双方start.js并insertnote.png都位于数据文件夹中.除了图像,一切都有效.我只是找不到如何获取图像标记的真实网址.相对网址似乎不起作用.:(
是否可以在内容脚本中包含插件的内部图像,或者我应该只使用绝对网址到我的网络服务器?
我一直以为浏览器会逐字逐句地从上到下执行JavaScript代码(你有点期望这种行为来自脚本语言).但显然情况并非如此:
//工作完美
<script>
test();
function test() { alert('test'); }
</script>
Run Code Online (Sandbox Code Playgroud)
但如果我将函数声明为变量,它将失败并显示'未捕获的ReferenceError:未定义测试':
<script>
test();
var test = function() { alert('test'); }
</script>
Run Code Online (Sandbox Code Playgroud)
所以javascript引擎有时不会从上到下执行代码.它可以以某种方式预加载函数,即使它们最终被声明.它究竟是如何工作的?为什么?
我有两个div:
<div id='div1'>
<span>text</span>
</div>
<div id='div2'>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图将跨度从div1移动到div2:
var div1 = document.querySelector('#div1');
var div2 = document.querySelector('#div2');
//cloning span
var span = div1.firstElementChild.cloneNode();
//removing span from the first div
div1.removeChild(div1.firstElementChild);
//trying to append new span to the second div but nothing happens
div2.appendChild(span);
Run Code Online (Sandbox Code Playgroud)
span元素已删除,但未插入第二个div中.
我错过了什么吗?