我刚刚开始使用原型JavaScript,并且我很难弄清楚this当范围发生变化时如何在原型函数内保留对主对象的引用.让我说明一下我的意思(我在这里使用jQuery):
MyClass = function() {
this.element = $('#element');
this.myValue = 'something';
// some more code
}
MyClass.prototype.myfunc = function() {
// at this point, "this" refers to the instance of MyClass
this.element.click(function() {
// at this point, "this" refers to the DOM element
// but what if I want to access the original "this.myValue"?
});
}
new MyClass();
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过以下方式执行此操作来保留对主对象的引用myfunc:
var myThis = this;
Run Code Online (Sandbox Code Playgroud)
然后使用myThis.myValue访问主对象的属性.但是当我有一大堆原型函数时会发生什么MyClass?我是否必须this在每个开头保存引用?似乎应该有一个更清洁的方式.这样的情况怎么样:
MyClass = function() {
this.elements $('.elements'); …Run Code Online (Sandbox Code Playgroud) 我正在定义一个这样的对象:
function Project(Attributes, ProjectWidth, ProjectHeight)
{
this.ProjectHeight = ProjectHeight;
this.ProjectWidth = ProjectWidth;
this.ProjectScale = this.GetProjectScale();
this.Attributes = Attributes;
this.currentLayout = '';
this.CreateLayoutArray = function()
{....}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建和实例像这样:
var newProj = new Project(a,b,c);
Run Code Online (Sandbox Code Playgroud)
但是这个例子被抛出:
Project is not a constructor
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?我搜索了很多,但仍然无法弄清楚我做错了什么.
我从基本的意义上理解JQuery,但我肯定是新手,并怀疑这很容易.
我在JSON响应(转换为对象)中得到了我的图像src和id,因此在responseObject.imgurl和responseObject.imgid中得到了正确的值,现在我想用它创建一个图像并将其附加到一个div(让我们称之为<div id="imagediv">.我有点坚持动态构建<img src="dynamic" id="dynamic">- 我见过的大多数例子涉及替换现有图像上的src,但我没有现有的图像.
我有一个隐藏的div包含一个YouTube视频<iframe>.当用户点击链接时,该div变为可见,然后用户应该能够播放视频.
当用户关闭面板时,视频应停止播放.我怎样才能做到这一点?
码:
<!-- link to open popupVid -->
<p><a href="javascript:;" onClick="document.getElementById('popupVid').style.display='';">Click here</a> to see my presenting showreel, to give you an idea of my style - usually described as authoritative, affable and and engaging.</p>
<!-- popup and contents -->
<div id="popupVid" style="position:absolute;left:0px;top:87px;width:500px;background-color:#D05F27;height:auto;display:none;z-index:200;">
<iframe width="500" height="315" src="http://www.youtube.com/embed/T39hYJAwR40" frameborder="0" allowfullscreen></iframe>
<br /><br />
<a href="javascript:;" onClick="document.getElementById('popupVid').style.display='none';">
close
</a>
</div><!--end of popupVid -->
Run Code Online (Sandbox Code Playgroud) 如何使用从字符串创建Web worker(通过POST请求提供)?
我能想到的一种方法,但我不确定如何实现它,是通过从服务器响应创建数据URI,并将其传递给Worker构造函数,但我听说有些浏览器不允许这,因为相同的原产地政策.
注意:作为Worker构造函数的参数传递的URI必须遵循同源策略.目前,浏览器供应商对数据URI是否来源不一致存在分歧; Gecko 10.0(Firefox 10.0/Thunderbird 10.0)及更高版本确实允许数据URI作为工作人员的有效脚本.其他浏览器可能不同意.
这里还有一篇关于whatwg讨论它的帖子.
我的扩展非常简单:
的manifest.json
{
"name": "historyCleaner",
"version": "0.1.1",
"manifest_version": 1,
"description": "This is my first Chrome extension",
"background": {
"scripts": ["cleaner.js"]
},
"permissions": [
"history"
]
}
Run Code Online (Sandbox Code Playgroud)
cleaner.js
chrome.history.onVisited.addListener(function(HistoryItem result) {
console.log("it works!");
alert("it works!");
});
Run Code Online (Sandbox Code Playgroud)
我已经将它加载到谷歌浏览器中,它已打开并且......它不起作用.它不会在控制台中记录任何内容,它不会提醒任何事情,更糟糕的是,我在开发人员工具"Scripts"选项卡中找不到它.我怎么才能找到它无效的原因?
//编辑
我把manifest.json改成了这个:
{
"name": "historyCleaner",
"version": "0.1.5",
"manifest_version": 1,
"description": "This is my first Chrome extension",
"background_page": "background.html",
"permissions": [
"history",
"background"
]
}
Run Code Online (Sandbox Code Playgroud)
并在background.html中嵌入了JavaScript
我想从webstore下载扩展名的.crx文件,当我从webstore安装扩展程序时,我使用fiddler来分析网络请求.
例如,对于扩展程序:https://chrome.google.com/webstore/detail/bjclhonkhgkidmlkghlkiffhoikhaajg
,下载链接为:
https://clients2.googleusercontent.com/crx/download/OgAAADQ_Loe5gfVPF2OUaB35tvex-NKlmA8V4K5YlWuvLCknMH7egLLmnMoFuCZePl_idE1GMf8jZC2KbjQqyyLDoDAAxlKa5eDp-z9frOppHWtQsRU3-iGrrrrA/extension_1_7_11.crx
现在我不知道是否有一个通用的方法得到的.crx扩展的,问题是如何做谷歌加密
bjclhonkhgkidmlkghlkiffhoikhaajg
成
OgAAADQ_Loe5gfVPF2OUaB35tvex-NKlmA8V4K5YlWuvLCknMH7egLLmnMoFuCZePl_idE1GMf8jZC2KbjQqyyLDoDAAxlKa5eDp-z9frOppHWtQsRU3-iGrrrrA ?
任何想法都会有所帮助.
google-chrome download google-chrome-extension chrome-web-store
有没有办法隐藏"XHR finished loading"控制台中的消息?
ajax console google-chrome xmlhttprequest google-chrome-devtools
我不想打开另一个" 又一个Js VS Js "主题.
我想,最后Prototype和jQuery都是JS,并且具有几乎相同的方法和功能,并且需要几乎相同的代码行来完成相同的任务.
对于真正的富Internet应用程序,Prototype与jQuery 的真正优势和缺点是什么?
在Chrome开发人员面板中,此工具...

允许您选择元素并在DOM视图中直接转到它们,并查看它们的CSS属性.
Safari拥有相同的工具.
是否有键盘快捷键来激活该工具(一旦打开面板)?
javascript ×6
jquery ×2
ajax ×1
background ×1
console ×1
constructor ×1
data-uri ×1
debugging ×1
download ×1
image ×1
object ×1
oop ×1
prototypejs ×1
ria ×1
scope ×1
this ×1
typeerror ×1
web-worker ×1
youtube-api ×1