在网络上我读到:
"Well, Javascript, or better ECMAScript, is an Object Based Language so.. Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..."
和:
When a function is invoked with “new” keyword then the function is known as constructor function and returns a new instance. In such cases, the value of “this” refers to newly created instance.
现在,我的代码:
function fe(){
var i;
var j = 0;
return j;
};
process.stdout.write("fe --> "); console.log(fe); // [Function: fe]
process.stdout.write("fe() --> "); console.log(fe()); // 0
var vfe = new fe();
process.stdout.write("vfe --> " + vfe + " - "); console.log(vfe); [object Object] - fd {}
var fd = function(){
var i;
var j = 0;
};
process.stdout.write("fd --> "); console.log(fd); // [Function: fd]
process.stdout.write("fd() --> "); console.log(fd()); // undefined
var vfd = new fd();
process.stdout.write("vfd --> " + vfd + " - "); console.log(vfd); [object Object] - fd {}
在第一种情况下,我返回了值,并且 new 返回了一个对象,最后一种情况是:我有一个对象 {},因此我可以附加属性和方法
在另一种情况下:
function fe(){
var i;
var j = 0;
return fe;
};
process.stdout.write("fe --> "); console.log(fe); // [Function: fe]
process.stdout.write("fe() --> "); console.log(fe()); // [Function: fe]
var vfe = new fe();
process.stdout.write("vfe --> "); console.log(vfe); // [Function: fe]
var fd = function(){
var i;
var j = 0;
return fd;
};
process.stdout.write("fd --> "); console.log(fd); // [Function: fd]
process.stdout.write("fd() --> "); console.log(fd()); // [Function: fd]
var vfd = new fd();
process.stdout.write("vfd --> "); console.log(vfd); // [Function: fd]
在这种情况下,我返回一个值或一个函数,但它只是一个对象...... 现在: id “任何不是原始类型(未定义、null、数字、字符串、 bool 值)的东西都是 JavaScript 中的对象(或实例),其中对象是属性和方法的“包”...”并非如此在所有情况下?
另一种情况如下:
function fe () {
var pri_var1;
// private, not accessible from extern
function pri_fun1(){ return "pri_fun1"; }
// private, not accessible from extern
function pri_fun2(){ return "pri_fun2"; }
//static property
fe.sta_var1 = 0; //static property ....is never a good idea but...for folks!!!
//static method
fe.sta_fun1 = function(){return pri_fun2();}
//public not heritable
this.pub_fun1 = function(){return "pub_fun1";}
//public heritable
fe.prototype.pub_her_fun1 = function() { return "pub_her_fun1";}
//return fe;
}
var obj2 = new fe();
console.log("obj2 --> ",obj2);
console.log(obj2.pub_fun1());
console.log(fe.sta_fun1());
如果我取消注释//return fe; “节点”说: console.log(obj2.pub_fun1()); ^
类型错误:obj2.pub_fun1 不是一个函数... 如果我留下评论,一切都正常......
关于函数和对象的一点困惑(再次):
"Well, Javascript, or better ECMAScript, is an Object Based Language so.. Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..."
提前致谢
好的...我会尽量说得更准确。 我读到“任何不是原始类型(未定义、空、数字、字符串、 bool 值)的东西都是 JavaScript 中的对象(或实例),其中对象是属性和方法的“包”......” 那么函数就是对象吗?每个功能? 取函数:
function fe(){
return 1;
};
以及以下说明:
console.log(fe); // [Function: fe]
console.log(fe()); // 1
var vfe = new fe();
console.log(vfe); // fe {}
如果我读得好: fe 是一个函数,它是非原始函数,是一个“对象”(什么样的对象?也许有点复杂的类型表示[函数:fe]以非 OO 方式不正确地调用对象?)...但它在 OO 中成为对象“bag”{} 的方式仅在 new (vfe).... 之后,但也可以被调用 (fe()) 并产生值 (1)...或者可以返回自身或默认情况下未定义。 .. 有些函数无法执行而只能“实例化”?其他函数可以包含经典函数来执行以获得正常值但不被实例化??? 非常非常奇怪的行为...... 我可以在命名、设计和边界方面获得帮助吗? 再次感谢...
最佳答案
当你调用一个函数作为构造函数(即new func()
)时,JS 会执行以下步骤:
- 它创建了一个新对象,我们称之为
newObject
- 它以特殊方式调用您的函数,以便函数内的
this
变为newObject
- 函数运行并返回一个值,我们称之为
retVal
- 现在 JS 会查看
retVal
。如果它什么都没有(即,未定义
)或原始值,则retVal
将被忽略(丢弃),并且整个new func()<的结果
的东西将是newObject
- 但是,如果
retVal
是一个对象,JS 的做法恰恰相反:newObject
被丢弃,并且new func()
的值> 将是retVal
这解释了你的结果,因为在第一种情况下,你返回一个基元 (0
),在第二种情况下,返回一个对象 (fd
,它是一个函数,函数是对象)。
插图:
function returnsPrimitive() {
this.x = 1
return 42
}
console.log(returnsPrimitive())
console.log(new returnsPrimitive())
function returnsObject() {
this.x = 1
return {foo:1}
}
console.log(returnsObject())
console.log(new returnsObject())
关于Javascript:函数、对象、new ....有点困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53485181/