据我所知,函数应该从其prototype对象继承属性,可以使用.prototype或__proto__属性访问它.
//my prototype Object
var myObj = {
a: 1,
b: 2
};
var myFunc = function () {};
// setting function's `prototype` property
myFunc.prototype = myObj;
alert(myFunc.a);
//returns undefined (Why???) I was expecting 1
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试以下,
//setting function __proto__ property
myFunc.__proto__ = myObj;
//returns 1
alert(myFunc.a);
Run Code Online (Sandbox Code Playgroud)
那么为什么它在我设置myFunc.__proto__时起作用而不是在我设置时起作用myFunc.prototype?
我确实参考了__proto__ VS. JavaScript中的原型但无法弄清楚.
是否可以在对象上设置默认函数,以便在调用myObj()该函数时执行?假设我有以下func对象
function func(_func) {
this._func = _func;
this.call = function() {
alert("called a function");
this._func();
}
}
var test = new func(function() {
// do something
});
test.call();
Run Code Online (Sandbox Code Playgroud)
我想test.call()简单地替换test().那可能吗?
将以下代码发布到Babel REPL中
class Test {
}
class Test2 extends Test {
}
Run Code Online (Sandbox Code Playgroud)
你得到这个inherits功能
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
Run Code Online (Sandbox Code Playgroud)
它看起来没什么问题,直到我意识到这是做既Object.create对原型和 …
看来这里有区别......
让我们说我们有 function MyConstructor() {}
MyConstructor [[Prototype]]是Function.prototype,而不是 MyConstructor.prototype.
在其他(非标准/"console.log-able")字样中:
MyConstructor.__ proto__ 不是 MyConstructor的MyConstructor.prototype
试试这个:
function MyConstructor() {};
(MyConstructor.__proto__ === MyConstructor.prototype); //false?! why?
Run Code Online (Sandbox Code Playgroud)
为什么会这样?有人能解释一下这个区别吗?
__proto__和之间有什么区别prototype
我在网上阅读了大部分文章,我仍然无法理解..据我所知
__proto__ ,原型对象的属性
prototype是实际对象我是否正确?....
为什么只有函数具有原型属性?它是如何成为一个对象?
var fn = function(){};
console.dir(fn);
Run Code Online (Sandbox Code Playgroud)
产量
Run Code Online (Sandbox Code Playgroud)function fn() arguments: null caller: null length: 0 name: "" prototype: Object __proto__: () <function scope>
使用对象和函数我尝试__proto__
在chrome控制台中设置值和原型,如下所示
//create object and display it
var o = {name : 'ss'};
console.dir(o);
Run Code Online (Sandbox Code Playgroud)
产量
Run Code Online (Sandbox Code Playgroud)Object name: "ss", __proto__: Object
//set the values
o.__proto__ = 'aaa';
o.prototype = 'bbb';
//after set the values display the object
console.dir(o);
Run Code Online (Sandbox Code Playgroud)
产量
Run Code Online (Sandbox Code Playgroud)Object name: "ss", prototype: "aaa", __proto__: Object
//create function and …Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关 javascript 原型链接的内容,据我了解,有一个全局变量Object.prototype是其他原型的基础,例如Array.prototype,它可以是另一个原型的基础。就像基于类的 OOP 中的继承一样。没关系。
现在,我想检查和比较不同对象的原型。如果Array的原型是基于 的Object.prototype,我想类似的事情Array.prototype.prototype应该是可能的。但它是未定义的:
> Array.prototype.prototype
undefined
Run Code Online (Sandbox Code Playgroud)
当我输入__proto__而不是 时prototype,我得到:
> Array.__proto__
[Function: Empty]
> Object.__proto__
[Function: Empty]
> Array.__proto__.__proto__
{}
Run Code Online (Sandbox Code Playgroud)
(控制台输出取自nodejs)。我有以下问题:
prototype和 和有什么不一样__proto__?该instanceof运营商应该看看样机,不是吗?在对象的原型发生变化后,为什么不改变它的答案?示例如下:
// The .prototype of objects created with 'new MyKlass'
// is MyKlass.prototype
var MyKlass = function(name, age) {
this.name = name;
this.age = age;
}
var xx = new MyKlass('xx', 20);
console.log(xx instanceof MyKlass); // true, OK
xx.prototype = new String('s');
console.log(xx instanceof MyKlass); // also true, WHY???
Run Code Online (Sandbox Code Playgroud) 我有这个功能:
function fff(){}
这是一个function它是一个实例Function constructor
所以fff.__proto__应该告诉我:function Function() { [native code] }
但事实并非如此.
表明 : function Empty() {}

这只是我看到的constructor财产__proto__function Function() { [native code] }
题 :
这个function Empty() {}功能是什么
,为什么fff.__proto__不告诉我:function Function() { [native code] } ?
注:我知道,__proto__是在查找链用来解决方法等实际的对象prototype是使用对象建立 __proto__,当你创建一个对象new.
但是又一次:函数fff是一个在幕后通过newing实例化的函数Function constructor.......所以?
好吧,我很想了解javascript中的原型并发现了很多文章,但是我无法理解为什么我不能在javascript中的Object常量中使用原型。众所周知,一切都是从Object继承的,所以在这种情况下
function Dog() {
}
Dog.prototype = new Animal;
Dog.prototype.bark = function() {
console.log("Woof! My name is " + this.name);
};
Run Code Online (Sandbox Code Playgroud)
如果我能够在函数中使用原型,为什么我不能在对象文字中使用原型,例如以下示例
var obj = {
firstname: 'foo',
lastname:'bar'
}
// this throws an error
obj.prototype.getMethod = function () {
console.log('this is a function');
}
Run Code Online (Sandbox Code Playgroud)
我经历了所有这些问题,但它确实无法回答为什么无法在JavaScript的对象文字中使用原型的原因。以下是一些参考
我有以下代码:
function Shape(x, y) {
this.x = x;
this.y = y;
}
Shape.prototype.describeLocation = function() {
return 'I am located at ' + this.x + ', ' + this.y;
};
var myShape = new Shape(1, 2);
function Circle(x, y, radius) {
Shape.call(this, x, y); // call parent constructor
this.radius = radius;
}
var myFirstCircle = new Circle(3, 4, 10);
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.calculateArea = function() {
return 'My area is ' + (Math.PI * this.radius * this.radius);
};
var mySecondCircle …Run Code Online (Sandbox Code Playgroud) javascript ×10
prototype ×5
inheritance ×3
function ×2
oop ×2
babeljs ×1
ecmascript-6 ×1
object ×1
prototypejs ×1