Cannot set property 'name' of undefined JS

This question already has an answer here: How does JavaScript .prototype work? 21 answers Objects don't have a prototype property (unless you create one). You usually only assign to the prototype property of constructors: function Person(firstname, lastname) { this.firstname = firstname; this.lastname = lastname; } Person.prototype.name = 'Toby'; var A = new Person('John', 'Doe')

无法设置未定义JS的属性“名称”

这个问题在这里已经有了答案: JavaScript .prototype如何工作? 21个答案 对象没有原型属性(除非你创建一个)。 您通常只分配给构造函数的prototype属性: function Person(firstname, lastname) { this.firstname = firstname; this.lastname = lastname; } Person.prototype.name = 'Toby'; var A = new Person('John', 'Doe'); // A.name === 'Toby';

Accessing Javascript object prototype

This question already has an answer here: How does JavaScript .prototype work? 21 answers You do have access to the prototype property, but it is only present on Function s. var car = { Make: 'Nissan', Model: 'Altima' }; This is the same as: var car = new Object(); car.Make = 'Nissan'; car.Model = 'Altima'; So, car.__proto__ === Object.prototype . And car.prototype === undefi

访问Javascript对象原型

这个问题在这里已经有了答案: JavaScript .prototype如何工作? 21个答案 您可以访问原型属性,但它仅存在于Function s上。 var car = { Make: 'Nissan', Model: 'Altima' }; 这与以下内容相同: var car = new Object(); car.Make = 'Nissan'; car.Model = 'Altima'; 那么, car.__proto__ === Object.prototype 。 car.prototype === undefined因为prototype属性只存在于Function s上(如我已经说过的)

prototypical inheritance javascript

Coming from a Java background, Javascript is a new world I'm trying to grasp. Im kind of struggeling with how prototypical inheritance exactly works. What i got from __proto__ VS. prototype in JavaScript and other sources was helpfull but I really want to make sure I grasp this topic. Are the following statements right? __proto__ , a property of objects, is an object which represents

原型继承javascript

来自Java背景的Javascript是我想要抓住的一个新世界。 我对原型继承如何精确地起作用感到困惑。 我从__proto__ VS得到了什么。 原型在JavaScript和其他来源是有帮助的,但我真的想确保我掌握这个话题。 以下说法正确吗? __proto__是对象的一个​​属性,它是一个代表对象原型的对象。 该对象反过来也可以具有__proto__属性,直到达到链的末尾的Object对象。 prototype是一个函数对象的属性,并且是一个对象本身。 当

Javascript Prototypal Inheritance Doubt II

I'been doing some inheritance in js in order to understand it better, and I found something that confuses me. I know that when you call an 'constructor function' with the new keyword, you get a new object with a reference to that function's prototype. I also know that in order to make prototypal inheritance you must replace the prototype of the constructor function with an ins

Javascript原型继承疑问二

为了更好地理解它,我在js中做了一些继承,并且发现了让我困惑的东西。 我知道当你用new关键字调用'构造函数'时,你会得到一个引用该函数原型的新对象。 我也知道,为了进行原型继承,您必须将构造函数的原型替换为您想成为“超类”的对象的实例。 所以我做了这个愚蠢的例子来尝试这些概念: function Animal(){} function Dog(){} Animal.prototype.run = function(){alert("running...")}; Dog.prototype = new

VS. prototype in JavaScript

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, and which in turn also references via its __proto__ property again to the Object.prototype. Thus, repeat, Foo.prototype is just an explicit property of Foo which refers to the prototype of b and c objects. var b = new Foo(20); var c = new Foo(30); What ar

VS. JavaScript中的原型

这个图再次表明每个物体都有一个原型。 构造函数Foo也有它自己的__proto__ ,它是Function.prototype,并且它又通过它的__proto__属性再次引用Object.prototype。 因此,重复一遍,Foo.prototype只是Foo的一个显式属性,它指向b和c对象的原型。 var b = new Foo(20); var c = new Foo(30); __proto__和prototype属性之间有什么区别? 这个数字是从这里取的。 __proto__是在查找链用来解决方法,实际的对象等prototype是用

javascript prototypal inheritance and the 'new' keyword

I've been playing around with prototypal inheritance in JavaScript and have been confused by the behavior of the new keyword. I cannot understand why the [[prototype]] property of the inheriting object points to Function.prototype and not the prototype of the inherited object. Consider 2 constructor functions (below): function Animal(name) { this.name = name; } function Cat(name) {

JavaScript原型继承和'new'关键字

我一直在玩JavaScript的原型继承,并且被new关键字的行为困惑。 我不明白为什么继承对象的[[prototype]]属性指向Function.prototype而不是继承对象的原型。 考虑2个构造函数(如下): function Animal(name) { this.name = name; } function Cat(name) { Animal.call(this, name); } Cat.prototype = new Animal(); 查询构造函数Cat的原型,我得到了一些有趣的结果: Cat.__proto__ === Animal.prototype; //retu

Copying Javascript getters/setters to another prototype object

// Base class var Base = function() { this._value = 'base'; }; Base.prototype = { constructor: Base, // By function getValue: function() { return this._value; }, // By getter get value() { return this._value; } }; // Sub class extends Base var Sub = function() { this._value = 'sub'; }; Sub.prototype = { constructor: Sub }; // Pass over metho

将Javascript getters / setters复制到另一个原型对象

// Base class var Base = function() { this._value = 'base'; }; Base.prototype = { constructor: Base, // By function getValue: function() { return this._value; }, // By getter get value() { return this._value; } }; // Sub class extends Base var Sub = function() { this._value = 'sub'; }; Sub.prototype = { constructor: Sub }; // Pass over metho

javascript prototype inheritance

This seems inconsistent, but probably is due to the fact that I'm new to javascript's prototype inheritance feature. Basically, I have two base class properties, "list" and "name". I instantiate two subclasses and give values to the properties. When I instantiate the second subclass, it gets the list values from the first subclass instance, but only for the "l

JavaScript原型继承

这看起来不一致,但可能是由于我是JavaScript的原型继承功能新手。 基本上,我有两个基类属性,“列表”和“名称”。 我实例化两个子类并给这些属性赋值。 当我实例化第二个子类时,它从第一个子类实例中获取列表值,但只针对“列表”而不针对“名称”。 这是怎么回事?? 当然,我宁愿任何后续的子类实例都不会从其他实例获取值,但如果发生这种情况,它应该是一致的! 这是一段代码片段: function A() { this

Understanding prototypal inheritance in JavaScript

I am new to JavaScript OOP. Can you please explain the difference between the following blocks of code. I tested and both blocks work. What's the best practice and why? First block: function Car(name){ this.Name = name; } Car.prototype.Drive = function(){ document.write("My name is " + this.Name + " and I'm driving. <br />"); } SuperCar.prototype = new Car(); SuperCar.pr

了解JavaScript中的原型继承

我是JavaScript OOP的新手。 你能解释下面的代码块之间的区别吗? 我测试过,并且两个块都可以工作 最佳做法是什么?为什么? 第一块: function Car(name){ this.Name = name; } Car.prototype.Drive = function(){ document.write("My name is " + this.Name + " and I'm driving. <br />"); } SuperCar.prototype = new Car(); SuperCar.prototype.constructor = SuperCar; function SuperCar(name){

Use of 'prototype' vs. 'this' in JavaScript?

What's the difference between var A = function () { this.x = function () { //do something }; }; and var A = function () { }; A.prototype.x = function () { //do something }; The examples have very different outcomes. Before looking at the differences, the following should be noted: A constructor's prototype provides a way to share methods and values among instan

在JavaScript中使用'原型'与'这个'?

有什么区别 var A = function () { this.x = function () { //do something }; }; 和 var A = function () { }; A.prototype.x = function () { //do something }; 这些例子的结果非常不同。 在查看差异之前,应该注意以下几点: 构造函数的原型提供了一种通过实例的private [[Prototype]]属性在实例之间共享方法和值的方法。 函数的这个由函数的调用方式或使用绑定来设置(这里没有讨论)。 在