`
kabike
  • 浏览: 606043 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

as的this和js的this

阅读更多
在js中,我们会随手写下这样的代码
<html> 
    <head>
        <script type="text/javascript">
           function bar(o){
           	alert(o);
           }
        </script>
    </head>
    <body>
      <input type="button" onclick="bar(this);" value="button">
   </body>
</html>


这时的那个this自然而然的是指的那个input,因为onclick="bar(this);"把"bar(this);"这个语句放到了那个input的
作用域链中.
再看同样的as代码
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   minWidth="955" minHeight="600">
	<fx:Script>
		<![CDATA[
			private function bar(o:Object):void{
				trace(o);
			}
		]]>
	</fx:Script>
	<s:Button label="bar" click="bar(this);"/>
</s:Application>

因为mxml文件会最终编译为as类,而this在最终的类文件中指的是那个类本身,因此,这里的this指的是这个application
不过要特别注意inline itemrenderer,因为它们最终会编译到各自的类里,因此inline itemrenderer的this不是当前组件,
而是itemrenderer本身.

下面这两个例子更能明显的说明问题
js的
<html> 
    <head>
        
    </head>
    <body>
      <input type="button" value="button" id="button">
      <span id="span">a span</span>
      <script type="text/javascript">
           function bar(){
           	alert(this);
           }
           var input=document.getElementById("button");
           input.addEventListener("click",bar);
           var span=document.getElementById("span");
           span.addEventListener("click",bar);
        </script>
   </body>
</html>


bar分别被attach到了一个input和一个span上,当bar函数分别执行的时候,它的变量作用域分别是对应的input和span
因此分别点击input和span的时候,this分别指向input和span

而在as中
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   minWidth="955" minHeight="600" creationComplete="init(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			private function bar(event:MouseEvent):void{
				trace(this);
			}
			
			protected function init(event:FlexEvent):void
			{
				button.addEventListener(MouseEvent.CLICK,bar);
				label.addEventListener(MouseEvent.CLICK,bar);
			}
			
		]]>
	</fx:Script>
	<s:Button label="bar" id="button"/>
	<s:Label text="label" id="label" y="40"/>
</s:Application>

this都是application本身
分享到:
评论

相关推荐

    as-this:用this指向第一个参数的javascript函数

    与调用javascript函数this提到的第一个参数 主要是CoffeeScript用'@'代替长变量名的语法糖: 安装 npm install as-this 用法 作为(自己,fn) 调用函数,返回self对象: as = require ' as-this ' self = {} ret ...

    as与js方法的相互调用

    在跨平台应用开发中,经常会遇到ActionScript (AS)与JavaScript (JS)之间的交互需求,尤其是在Flex(一种基于Adobe Flash的开发框架)的应用中。Flex是使用ActionScript编写,而网页环境通常由HTML和JavaScript控制...

    Javascript中神奇的this

    在没有特定上下文的情况下,如全局环境中直接调用函数,`this`会默认绑定到全局对象(浏览器环境下是`window`,Node.js中是`global`)。在严格模式下,`this`会绑定到`undefined`。 ```javascript function fn() { ...

    如何使用Javascript中的this关键字

    在不同的场景下,`this`的指向会发生变化,这使得它成为JavaScript中理解和使用的一个关键点。 一、基础理解 1. 全局函数中的`this` 如`doSomething`函数所示,如果一个函数作为全局函数定义,`this`默认指向全局...

    Learn JavaScript with p5.js_Coding for Visual Learners-Apress(2018).pdf

    This book will present various JavaScript and p5.js features and concepts in the following chapters. The knowledge will be reinforced by building several useful examples like an animation and a data...

    Server Side development with Node.js and Koa.js

    Modern versions of JavaScript have made this possible in Node.js, and Koa is a Node.js framework that makes it easy. This book is the ideal introduction for JavaScript developers who want to create ...

    javascript中的self和this用法小结

    一、 起因 那天用到prototype.js于是...代码如下:var Class = { create: function() { return function() { this.initialize.apply(this , arguments); } } } // Class使用方法如下var A = Class.create(); A. prototy

    javascript的this关键字详解

    JavaScript中的`this`关键字是一个非常重要的概念,它用于表示当前执行上下文的对象。根据不同的环境,`this`的指向会有所不同...通过深入学习`this`的原理和应用场景,开发者能够更好地掌握JavaScript的面向对象编程。

    Learning JavaScript Design Patterns 英文原版.js设计模式

    If you want to write beautiful, structured, and maintainable JavaScript code, this guide shows you how to apply both classical and modern design patterns to the language. The patterns in this book ...

    老生常谈Javascript中的原型和this指针

    1、Javascript中的原型: 原型prototype是Javascript中特有的一个概念。通过原型,Javascript可以实现继承机制。 Javascript本身是基于原型的,每一个对象都有一个prototype属性。而Object对象的prototype属性为null...

    javascript中this的四种用法

    在《javaScript语言精粹》这本书中,把 this 出现的场景分为四类,简单的说就是: 有对象就指向调用对象 没调用对象就指向全局对象 用new构造就指向新对象 通过 apply 或 call 或 bind 来改变 this 的所指。 1) 函数...

    Object.Oriented.JavaScript

    This book explores JavaScript for what it is: a highly expressive and lexible prototype-based object-oriented programming language. Once dismissed as a toy for designers to make things such as ...

    【JavaScript源代码】使用 React 和 Threejs 创建一个VR全景项目的过程详解.docx

    首先,我们要知道React是一个用于构建用户界面的JavaScript库,而Three.js则是一个3D库,它提供了丰富的功能来创建三维图形和交互式体验。将两者结合,我们可以为用户创建一个沉浸式的720度全景图像浏览体验。 **一...

    JavaScript: Moving to ES2015

    This course offers an expert's eye on the latest ES6 features and how these advanced tasks fit together in JavaScript as a whole Discover robust JavaScript implementations of classic and advanced ...

    Angular.JS中的this指向详解

    在JavaScript和Angular.js中,`this`关键字是一个非常重要的概念,它表示当前执行上下文的对象。理解`this`的指向是编写高效、无bug代码的关键。以下是对`this`指向的详细解释: 首先,我们要明白两个核心原则: 1...

    javascript中onclick(this)用法介绍

    input id=”myinput” type=”text” value=”javascript中onclick中的this” onclick=”[removed]test(this);”/&gt; 代码如下: function test(obj){ alert&#40;obj&#41;; //[object HTMLInputElement] alert&#40;...

    Programming JavaScript Applications

    By applying the design patterns outlined in this book, you’ll learn how to write flexible and resilient code that’s easier—not harder—to work with as your code base grows., JavaScript has become ...

    Javascript的this详解

    在理解javascript的this之前,首先先了解一下作用域。 作用域分为两种: 1、词法作用域:引擎在当前作用域或者嵌套的子作用域查找具有名称标识符的变量。(引擎如何查找和在哪查找。定义过程发生在代码书写阶段) ...

Global site tag (gtag.js) - Google Analytics