该图再次显示每个对象都有一个原型.构造函数Foo也有自己
__proto__的Function.prototype,它又通过其__proto__属性再次引用到Object.prototype.因此,重复,Foo.prototype只是Foo的一个显式属性,它指的是b和c对象的原型.
var b = new Foo(20);
var c = new Foo(30);
Run Code Online (Sandbox Code Playgroud)
__proto__和prototype属性有什么区别?

这个数字来自这里.
javascript prototype prototypal-inheritance javascript-objects
我有一个Javascript类(在ES6中)已经很长了.为了更好地组织它,我想将它分成2或3个不同的文件.我怎样才能做到这一点?
目前它在单个文件中看起来像这样:
class foo extends bar {
constructor(a, b) {} // Put in file 1
methodA(a, b) {} // Put in file 1
methodB(a, b) {} // Put in file 2
methodC(a, b) {} // Put in file 2
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在Javascript中看到了这两种声明方法的方法:
var User = function() {
this.name = 'Foo';
this.greet = function() {
console.log('Hello!');
}
}
Run Code Online (Sandbox Code Playgroud)
和
var User = function() {
this.name = 'Foo';
}
User.prototype.greet = function() {
console.log('Hello!');
}
Run Code Online (Sandbox Code Playgroud)
有什么区别?
我尝试提高我的JavaScript技能.我不明白为什么(5)有效,(2)返回错误.不一样吗?
a.fn2()//好的
var A = function () {
this.fn = function () { alert(3); }
}
A.prototype = {
fn2: function () { alert(4); }
};
var B =
{
fn: function () { alert(1); }
}
B.prototype = {
fn2: function () { alert(2); }
};
Run Code Online (Sandbox Code Playgroud)当我想Object在JavaScript中向类中添加方法时,我遇到了一个问题.我不知道之间的不同Object.myMethod和Object.prototype.myMethod.
Object.myMethod = function (){};
Object.prototype.myMethod = function (){};
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗
所以假设你有一个非常基本的人物对象,有两个值和一个函数:
function personObject() {
this.name = 'First Name';
this.placeInLine = 1;
this.setPlaceInLine = function(place) {
this.placeInLine = place;
}
}
Run Code Online (Sandbox Code Playgroud)
我们设置了一些这样的变量:
var john = new personObject();
var bill = new personObject();
var message = "";
Run Code Online (Sandbox Code Playgroud)
现在看看下面的三个代码片段......
---代码#1 ---
if(john.placeInLine < bill.placeInLine) message = "John is before Bill";
else message = "John is not before Bill";
Run Code Online (Sandbox Code Playgroud)
结果:消息="约翰不在比尔之前"; //因为1不小于1
---代码#2 ---
bill.setPlaceInLine(2); // change Bill's place to 2 (instead of default of 1)
if(john.placeInLine < bill.placeInLine) message = "John is before Bill"; …Run Code Online (Sandbox Code Playgroud) 我怎么能把setPosition放在类中DrawClockHand?是DrawCLockHand即使在Javascript技术的一类.
码:
var DrawClockHand = function(cv) {
this._cv = cv;
this._ctx = cv.getContext('2d');
this.TIME = 1000;
this._r = 50;
}
DrawClockHand.prototype.setPosition = function(x,y) {
x = x || 0;
y = y || 0;
this._x = x;
this._y = y;
}
Run Code Online (Sandbox Code Playgroud)