本篇文章给大家谈谈JavaScripArrowFunctionsandThis,同时本文还将给你拓展CallingFlex/ActionscriptfunctionsfromJavascript、Fu
本篇文章给大家谈谈JavaScrip Arrow Functions and This,同时本文还将给你拓展Calling Flex / Actionscript functions from Javascript、Function Definition, This and Bind in JavaScript、function, new function, new Function之间的区别_javascript技巧、functional继承模式 摘自javascript:the good parts_javascript技巧等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- JavaScrip Arrow Functions and This
- Calling Flex / Actionscript functions from Javascript
- Function Definition, This and Bind in JavaScript
- function, new function, new Function之间的区别_javascript技巧
- functional继承模式 摘自javascript:the good parts_javascript技巧
JavaScrip Arrow Functions and This
我正在尝试ES6,并希望像这样在我的函数中包含一个属性
var person = { name: "jason", shout: () => console.log("my name is ", this.name)}person.shout() // Should print out my name is jason
但是,当我运行此代码控制台时,仅显示log my name is
。我究竟做错了什么?
答案1
小编典典简短的答案:this
指向最接近的边界this
-所提供的代码this
位于封闭范围内。
更长的答案:箭头功能 没有this
创建它们时将其绑定this
,arguments
或结合其它特殊名字都正在创建对象时的名称this
是在封闭的范围内,没有找到person
对象。您可以通过移动声明来更清楚地看到这一点:
var person = { name: "Jason"};person.shout = () => console.log("Hi, my name is", this);
并且在翻译成ES5中箭头语法的模糊近似时更加清晰:
var person = { name: "Jason"};var shout = function() { console.log("Hi, my name is", this.name);}.bind(this);person.shout = shout;
在这两种情况下,this
(对于shout函数)都指向与其中person
定义的作用域相同的函数,而不是将函数添加到person
对象时附加的新作用域。
您 不能
使箭头函数那样工作,但是,正如@kamituel在他的答案中指出的那样,您可以利用ES6中较短的方法声明模式来节省相似的空间:
var person = { name: "Jason", // ES6 "method" declaration - leave off the ":" and the "function" shout() { console.log("Hi, my name is", this.name); }};
Calling Flex / Actionscript functions from Javascript
Nowadays I am working with Flex projects,that runs on Lotus Notes platform,which consumes data from Lotus Notes backend. Since there is no remote services like BlazeDS to connect to Notes server,am Now greatly dependant on HTTPService and Javascript. Calling a javascript function from Flex is quite easy. Just use the ExternalInterface API. For those who don’t kNow the way,this is how it is getting called.
In AS3
if(ExternalInterface.available){
ExternalInterface.call(“openNotes”,parameter);
}
In Javascript
function openNotes(notesUrl){
window.open(notesUrl,”,‘width=1000,height=600′);
}
It is quite easy. But what if you need to call the reverse. ie,calling actionscript function from javascript. We can use the sameExternalInterface api for achieve this. There is a method calledaddCallback,available in ExternalInterface. addCallback method registers an ActionScript method as callable from the container. After a successful invocation of addCallBack()
,the registered function in the player can be called by JavaScript or ActiveX code in the container. The first parameter is the name by which the container can invoke the function and the second parameter is the function closure to invoke. Below is the step-by-step configuration:
Step 1 : Register the call back from actionscript. For eg,call the below method in the creationComplete or initialize event of the component.
private function createCallBack(event:Event):void{
ExternalInterface.addCallback(“updateNotes”,getNotes);
}private function getNotes():void{
//do whatever you want after getting the hold
}
Step 2 : Create logic in javascript to invoke the AS3 function.
//Javascript
function updateFlex(){
appName.updateNotes();
}
The appName is the name and id of the embedded swf object in the HTML. Like below :
…
That’s it. Now you can call the updateFlex javascript method from your HTML and it will invoke the AS3 callback function. Enjoy Coding guys. Flex Your Life. Cheers.
Reference:
http://deviltechie.wordpress.com/2012/05/08/calling-flex-actionscript-functions-from-javascript/
Function Definition, This and Bind in JavaScript
I thought I know the Function definition, execution context and the behavior of this in JavaScript. However, I realized that actually I don''t or the knowlege is still not firmly grounded in my mind when I wrote some code similar to below snippet but have no instinct of the error.
var TestObj = {
a: function() {
console.log(''A'');
},
b: function() {
console.log(''B'');
this.a();
}
};
TestObj.b();
var c = TestObj.b;
c();
The result will be as below, right?
B
A
B
A
You might suspiciously answer No but If your instint doesnot tell you that and why, then you don''t know JavasScript well either like me. The result actually is:
B
A
B
TypeError: Object [object global] has no method ''a''
It is a little bit awkward or counterintuitive at first glance but it''s JavaScript. It''s the feature and amazing part. Let''s break it down piece by piece and see why.
Function definition
The TestObj includes two methods. The Function definition there actually creates two anonymous functions and then the references to the functions are assigned to the properties a and b. Those two functions are not owned by TestObj and just referred by the two properties of TestObj. This is the most important part causes the confusion. Hence, above code has not much difference than below except now we assign a name B for one of the function:
function B() {
console.log(''B'');
this.a();
};
var TestObj = {
a: function() {
console.log(''A'');
},
b: B
};
this
In ECMA-262 edition 5.1:
10.4.3 Entering Function Code
The following steps are performed when control enters the execution context for function code contained in
function object F, a caller provided thisArg, and a caller provided argumentsList:
- If the function code is strict code, set the ThisBinding to thisArg.
- Else if thisArg is null or undefined, set the ThisBinding to the global object.
- Else if Type(thisArg) is not Object, set the ThisBinding to ToObject(thisArg).
- Else set the ThisBinding to thisArg.
...
this is a special keyword refers to the binding object in the current execution context of the Function.
Once we invoke the Function through Object method, the this inside the Function body actually has been set to the TestObj instance. Hence, TestObj.b() logs B and A consecutively because this.a exists as a property of TestObj.
However, below statements mean differently.
var c = TestObj.b;
c();
Actually, variable c is just another reference pointing to Function B. Hence c() is same as B(). When directly invoking Function B, the this is bound to global object. Because there is no a defined in the global object, error occurs.
How to set a particular object as this to function
It''s commonly known that call and apply method can be called on the Function object providing a specific object as this, say:
var c = TestObj.b;
c.call(TestObj);
The result is desirable. However, this approach invokes the Function immediately. This is normally not the case that a Function has to be assigned to a Reference and passed around which is meant to be executed dynamically, like:
function dynamic(fn) {
fn();
}
dynamic(TestObj.b);
In this case, we should not use fn.call(TestObj) or fn.apply(TestObj) because it''s a generic Function which should have no knowledge on the Function passed in. Hence, above is not working.
There is still another lifesaver though. The bind method of Function. This method can take the passed in Object like what call or apply does, but it returns a new Function whose this binding is set to the Object passed in. So, above code can be revised as:
function dynamic(fn) {
fn();
}
dynamic(TestObj.b.bind(TestObj));
It''s fun, isn''t it?
[Edited on 2013/06/17]: Today, I saw another case which maybe confusing too.
var length = 3;
function logLength() {
console.log(this.length);
}
var TestObj = {
length: 2,
b: logLength,
c: function() {
(function(fn) {
arguments[0]();
})(logLength);
}
};
TestObj.b();
TestObj.c();
What do you think the console should log? Will it be 2 and 3? Actually, the result is 2 and 1. Because the TestObj.c() actually is calling the function logLength on the arguments Object, and then the this.length is referring to its own length, which is 1.
More fun, right?
function, new function, new Function之间的区别_javascript技巧
函数是JavaScript中很重要的一个语言元素,并且提供了一个function关键字和内置对象Function,下面是其可能的用法和它们之间的关系。
使用方法一:
var foo01 = function() //or fun01 = function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
alert(typeof(foo01));
alert(foo01());
function
300 最普通的function使用方式,定一个JavaScript函数。两种写法表现出来的运行效果完全相同,唯一的却别是后一种写法有较高的初始化优先级。在大扩号内的变量作用域中,this指代foo01的所有者,即window对象。
使用方法二:
var foo02 = new function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
alert(typeof(foo02));
alert(foo02.constructor());