var testvar = 'window属性';
var o3 = {
testvar:'3',
testvar2:'3**',
fun:function(){
alert('o3: '+this.testvar);//'obj3'
var inner = function(){
alert('o3-inner: '+this.testvar);//'window属性'
alert('o3-inner: '+this.testvar2);//undefined(未定义)
};
inner();
}
};
o3.fun();
调用函数时,你可以把this
想象为每个函数内的一个特殊(躲起来的)参数。无论什么时候,JavaScript都会把this
放到function内部。它是基于一种非常简单的思想:如果函数直接是某个对象的成员,那么this
的值就是这个对象。如果函数不是某个对象的成员那么this
的值便设为某种全局对象(常见有,浏览器中的window对象)。下面的内部函数可以清晰的看出这种思想。
Since the Window object is the global object in client-side
JavaScript, all global variables are defined as properties of the window. For
example, the following two lines of code perform essentially the same function:
var answer = 42; // Declare and initialize a global variable
window.answer = 42; // Create a new property of the Window object
span.meecallWrapper { font-size:1em; color:#B0E0E6; text-decoration:none; } a.meecallLink { color:#000000; text-decoration:none; } span.meecallInLink:hover { background-color:#B0E0E6; }
引用js文件的好处:
When
JavaScript functions
are used by
more than one page, placing them in a separate JavaScript file allows them to be
cached by the browser, making them load more quickly. When JavaScript code is
shared by multiple pages, the time savings of caching more than outweigh the
small delay required for the browser to open a separate network connection to
download the JavaScript file the first time it is requested.
setCapture函数的作用就是将后续的mouse事件都发送给这个对象,releaseCapture就是将鼠标事件还回去,由
document、window、object之类的自行来处理,这样就保证了在拖动的过程中,不会由于经过了其它的元素而受到干扰。另外,还有一个很重
要的事情是,在Win32上,mouse
move的事件不是一个连续的,也就是说,并不是我们每次移动1px的鼠标指针,就会发生一个mousemove,windows会周期性检查mouse
的位置变化来产生mousemove的事件。所以,如果是一个很小的页面对象,比如一个直径5px的圆点,如果没有setCapture和
releaseCapture,那么在鼠标按住之后,快速的移动鼠标,就有可能鼠标移动走了,但是小圆点还在原地,就是因为下一次的mousemove事
件已经不再发给这个圆点对象了。
<input type="button" value="Click Here" onclick="alert(typeof this.onclick);">
If you click the button, it displays a dialog box containing
the word "function," not the word "string." (Note that in event handlers, the
this
keyword refers to the object on which the event occurred.
<script>
// This function recursively looks at Node n and its descendants,
// replacing all Text nodes with their uppercase equivalents.
function uppercase(n) {
if (n.nodeType == 3 /*Node.TEXT_NODE*/) {
// If the node is a Text node, create a new Text node that
// holds the uppercase version of the node's text, and use the
// replaceChild( ) method of the parent node to replace the
// original node with the new uppercase node.
var newNode = document.createTextNode(n.data.toUpperCase( ));
var parent = n.parentNode;
parent.replaceChild(newNode, n);
}
else {
// If the node is not a Text node, loop through its children
// and recursively call this function on each child.
var kids = n.childNodes;
for(var i = 0; i < kids.length; i++) uppercase(kids[i]);
}
}
</script>
<!-- Here is some sample text. Note that the <p> tags have id attributes. -->
<p id="p1">This <i>is</i> paragraph 1.</p>
<p id="p2">This <i>is</i> paragraph 2.</p>
<!-- Here is a button that invokes the uppercase( ) function defined above. -->
<!-- Note the call to document.getElementById( ) to find the desired node. -->
<button onclick="uppercase(document.getElementById('p1'));">Click Me</button>
分享到:
相关推荐
//声明,并且赋值,即定义了 下面是几点总结: 变量的作用域:全局的和局部的。(注意:如果尝试读取一个未声明的变量的值,javascript会生成一个错误) 第一点:在都使用var关键字修饰变量的情况下,如果给一个局部...
javascript学习笔记发放2。这章我们 继续.然后了解下js中操作数据 和 函数的 作用域。
在Javascript,全局环境本身就一个对象。... 言归正传,当我们写下:var i=1时,其实就是声明了一个window作用域的一个变量。 而当我们写下i=1时,是声明了一个window的属性。 看这样一段代码: [Ctrl+A 全选 注:
在C和Java中,都有一个程序的入口函数或方法,即main函数或main方法。而在JavaScript中,程序是从JS源文件的头部...具体来说,在执行流程进入函数时会建立一个新的作用域,在函数执行完成退出时会销毁这个作用域。函
在之前的介绍中,我们已经知道 Javascript 没有块级作用,只有函数级作用域。 代码如下: function test() { // a scope for(var i = 0; i < 10; i++) { // not a scope // count } console.log(i); // ...
这是一份关于JavaScript ES6 的整理笔记,包括块级作用域,解构,promise,class,proxy
前篇文章我们介绍了学习javascript所需要的基础中的基础知识,今天我们来更进一步,学习下javascript变量及其作用域,希望小伙伴们通过本文能够有所得。
javascript从入门到跑路—–小文的js学习笔记(1)———script、alert、document。write() 和 console.log 标签 … … javascript从入门到跑路—–小文的js学习笔记目录 ** 在说函数前,补充一个函数的作用域的知识...
本文主要讲述了javascript中作用域和命名空间的区别,十分的详细,这里推荐给大家,希望小伙伴能有所收获
-_-||| 作用域 JS中只存在一种作用域—-公用作用域,所有对象的所有属性和方法是公用的。许多开发者都在网上提出了有效的属性作用域模式,解决了ECMAScript的这种问题。由于缺少私有作用域,开发者们制定了一个规约...
高级函数 1.1 作用域安全的构造函数 ①直接调用构造函数而不适用new操作符时,由于this对象的晚绑定,它将映射在全局对象window上,导致对象属性错误增加到window。 代码如下: function Person(name,age,job){ this....
基础篇 Javascript学习笔记1 数据类型 Javascript学习笔记2 函数 Javascript学习笔记3 作用域 Javascript学习笔记4 Eval函数 Javascript学习笔记5 类和对象 Javascript学习笔记6 prototype的提出 Javascript学习...
面向对象的 javascript 学习 大家好,根据我的学习...引用,函数重载和类型检查,作用域,图解prototype和constructor, 闭包,上下文,公共方法和私有方法,公共变量私有变量,特权方法,静态方法,命名空间,编码建议
一.关于Javascript变量声明 在Javascript中,声明一个变量 var a=1; 也可以直接 a=1; 这两种表达是有区别的, 一个是当前作用域的局部变量,另一个则是当前作用域的全局变量;... // error 二.Javascript变量作用域
JavaScript的函数是基于词法作用域的顶级对象。Javascript是第一个成为主流的Lambda语言。相对于Java而言,JavaScript于Lisp和Scheme有更多的共同点。它是披着C外衣的Lisp。这使得JavaScript成为一个非常强大的语言...
因为 Javascript 没有块级作用域,只有函数作用域,所以闭包的使用与函数是紧密相关的。 模拟私有变量 代码如下: function Counter(start) { var count = start; return { increment: function() { count++...
今天学习笔记主要有这样几个关键字:变量、参数传递、执行环境、变量对象、作用域链。 1.变量 变量需要注意的有两点:变量声明和复制变量值。 变量声明肯定大家都很熟悉,在JS中我们都是通过 var 关键字...