一个学生问我为什么JavaScript在变量声明之后需要分号,而在函数声明之后不需要分号,我没有一个很好的答案。
例如,这些变量声明(包括包含函数的声明)后面跟着分号.
var x = 5;
var test = function() { return null; };
但是这个函数声明之后没有分号,也不应该有分号。为什么?分化背后的逻辑是什么?为什么变量赋值需要分号,而函数声明不需要分号?
function test {
return null;
}
如果我将一个命名函数赋给一个变量,为什么我不能访问这个命名函数:
var a = function b() {
console.log("Hello World");
}
a() // hello world;
b() // b is not defined
b // b is not defined
此时,我不能删除a变量,但我可以删除b,当我删除b时,a的函数仍然存在
delete a //false
delete b //true
a() //Hello World
b函数不仅由a引用,也不是由copy引用,那么为什么a函数仍然存在呢?
几周前,我很痛苦地能够动态地向具有自己的.on的HTML对象添加按钮(单击)。处理程序,并使用e.stopPropgation阻止这些新的子元素触发事件。
我做的奇怪的事是调用一个没有括号的函数。我不知道为什么我这样做,为什么它工作,或为什么它不工作,当我附加括号。我想知道我是不是在做侥幸的事情,而不是设计(现在我要给它添加评论)。
它是这样的:
//Container is the parent element
// var buttons stores the buttons with class 'buttons'
$('.container').on(&
以下内容摘自Google开发人员控制台
typeof Object // type of Object is function (most confusing part).
"function" //Same fot all build-in types
Object.constructor
function Function(){[native code]}// Why not function Object()?
Object.hasOwnProperty("create") // Here, it is Object since it
我在可排序类上测试Jakobs补丁, this.reset()给了我一个Uncaught TypeError: undefined is not a function。
我不明白为什么自从班。
因此,我的解决方案是在同一个end:方法()中调用一个self.reset();,并在与以前的this.reset();相同的行中调用self.reset();。效果很好。为什么?
然后,为了检查(我已经怀疑了),我做了一个console.log(this == self)并给了false。
为什么使用self而不使用this?
我试图通过将函数缓存到变量来使我的代码更小。例如:
function test(){
var a = Array.prototype.slice,
b = a.call(arguments);
// Do something
setTimeout(function(){
var c = a.call(arguments);
// Do something else
}, 200);
}
因此,我可以只调用a.call(arguments);,而不是调用Array.prototype.slice.call(arguments)。
我试图通过缓存A
我正在读一段代码,我遇到了一个名为__toString的函数,这里是函数原型:
public function __construct($par)
现在我不知道为什么编码器选择__construct而不是construct作为函数名。如果有人能帮我解决这个问题,我将非常感激。
我读过问题,它说私有和受保护的函数可以以下划线开头,但它是公共函数。请帮助我理解为什么在这种情况下编码器选择使用下划线。谢谢
为什么这是不可能的?当使用构造函数参数实例化"DerivedClass“时,我得到以下编译器错误:
'GenericParameterizedConstructor.DerivedClass‘不包含采用1个参数的构造函数
但是调用一个非常类似的方法是可行的。
为什么?
class Program
{
static void Main(string[] args)
{
// This one produces a compile error
// DerivedClass cls = new DerivedClass("S
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
class A {
public:
A () {cout<<" A\n";}
virtual ~A () {cout<<" ~A\n";}
};
class B : public A {
有人能解释一下为什么,如果我在_start返回语句中添加parens,就会得到错误:
undefined is not a function
_start是函数,不是吗?
.factory('TrackPage', ['$location', function($location){
var self = this;
function _start(){
//method body
}
function _end(){
//method body
}
return {
目前我正在阅读“C++加速”,在第4章中的一个示例中,编写了一个函数来读取输入流,如下所示:
istream &read ( istream & in , vector < double >& work )
{
if ( in ) { // why it is needed ?
double x ; // x denotes the grade
work.clear ( ); // why we need it ?
while ( in >> x
如果我已经在使用外部函数,为什么需要定义自调用匿名函数?
让我们用这些简单的例子..。
为什么要这样做:
var countDown=60;
function myTimer()
{
document.getElementById("timer").innerHTML=countDown;
countDown--;
}
var stopper=setInterval(function(){myTimer()},1000);
但这并不是:
var countDown=60;
function myTimer()
{
document.getEleme