Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不';两个变量都没有定义吗?_Javascript_Node.js - Fatal编程技术网

Javascript 为什么不';两个变量都没有定义吗?

Javascript 为什么不';两个变量都没有定义吗?,javascript,node.js,Javascript,Node.js,为什么这些行不显示console.log(VipUser)和console.log(DummyUser)两者都抛出错误?如果在一个数据库中运行此代码,为什么不同时显示“undefined” 浏览器 (函数(){ var User=函数(名称、年龄){ this.name=名称; 这个。年龄=年龄; }; User.prototype.welcome=函数(){ console.log('Welcome'+this.name); }; User.prototype.getAge=函数(){ 返回这

为什么这些行不显示console.log(VipUser)和
console.log(DummyUser)两者都抛出错误?如果在一个数据库中运行此代码,为什么不同时显示“undefined”
浏览器

(函数(){
var User=函数(名称、年龄){
this.name=名称;
这个。年龄=年龄;
};
User.prototype.welcome=函数(){
console.log('Welcome'+this.name);
};
User.prototype.getAge=函数(){
返回这个年龄;
};
控制台日志(VipUser);
console.log(DummyUser);
函数DummyUser(姓名){
this.name=名称;
this.姓氏=姓氏;
this.toString=函数(){
返回this.name+“”+this.姓氏;
}
}
DummyUser.prototype.ToString=function(){
返回this.name+“”+this.姓氏;
}
var VipUser=函数(名称、年龄、成员ID){
};
}());

您需要像这样移动
控制台.log()

(function() {
var User = function(name, age){
    this.name = name;
    this.age = age;
};
User.prototype.welcome = function(){
    console.log('Welcome ' + this.name);
};
User.prototype.getAge = function(){
    return this.age;
};

function DummyUser(name, surname) {
    this.name = name;
    this.surname = surname;
    this.toString = function() {
        return this.name + ' ' + this.surname;
    }
}
DummyUser.prototype.toString2 = function() {
    return this.name + ' ' + this.surname;
}
var VipUser = function(name, age, memberId){

};
console.log(VipUser);
console.log(DummyUser);
}());
看到这个了吗

如果将匿名函数分配给变量,则在执行分配后才能访问它。换句话说,即使匿名函数已经“编译”,在赋值之前它是未定义的

从小提琴上:

console.log(x);
console.log(hello);

var x = function(t){
        if(t){
      return true;
    }
    return false;
}

function hello(r){
        if(r){
      return true;
    }
    return false;
}

console.log(x);
console.log(hello);
示例代码的前两个日志的输出为:

未定义

你好(r){ if(r){ 返回true; } 返回false; }

然后,第二个两个日志输出:

(t){
if(t){
返回true;
}
返回false;
}
你好(r){
if(r){
返回true;
}
返回false;
}


也许你应该试着让标题更具描述性。每个提出问题的人都想要某种解决方案。你为什么会期望错误?您遇到的实际问题是什么?请不要破坏您自己的问题。这两个符号都已定义。嗯,
DummyUser
被提升,因此
console.log()
应该打印函数。有关进一步的参考,请参阅。@triku听起来完全是另一个问题,请创建一个新问题。如果这回答了你的问题,请投票并接受它。