- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当我有我想用作构造函数的函数时,请说:
function clog(x){
var text = x;
return console.log(text );
}
var bla = new clog();
clog.prototype.alert = alert(text);
clog.alert = alert(text);
clog
的对象继承吗?是他们的原型(prototype)吗?
最佳答案
由构造函数创建的实例(在您的情况下为 clog
)继承对 clog.prototype
的引用。目的。因此,如果您将属性添加到 clog.prototype
,它将显示在实例上。如果您向 clog
添加属性本身,它不会出现在实例上。
您引用的代码存在一些问题,因此让我们看一个抽象示例:
function Foo() {
}
Foo.prototype.bar = "I'm bar on Foo.prototype";
Foo.bar = "I'm bar on Foo";
var f = new Foo();
console.log(f.bar); // "I'm bar on Foo.prototype"
// E.g., `f` inherits from `Foo.prototype`, not `Foo`
// And this link is live, so:
Foo.prototype.charlie = "I'm charlie on Foo.prototype";
console.log(f.charlie); // "I'm charlie on Foo.prototype";
I don't understand why new properties added directly to
Foo
would be ignored by the prototype chain?
Foo.prototype
,不是
Foo
,这是通过
new Foo()
创建的对象的原型(prototype).
isn't
prototype
simply points to the constructor object?
Foo
和
Foo.prototype
是完全不同的对象。
Foo
是一个函数对象,它像所有函数对象一样可以有属性。
Foo
之一的属性是
prototype
,这是一个非函数对象,除了
constructor
之外最初是空白的指向
Foo
的属性.它是
Foo.prototype
,不是
Foo
,通过
new Foo
创建的实例得到他们的原型(prototype)。
Foo
的唯一作用是创建使用
Foo.prototype
的对象。作为他们的原型(prototype)。 (实际上,在
Foo
的情况下,它只是初始化这些对象;它们由
new
运算符创建。使用像
Foo
这样的传统函数,
new
创建对象。如果此代码使用 ES2015+
class
语法,
new
不会创建对象,它将留给
Foo
[如果
Foo
是基类构造函数] 或
Foo
的最终基类 [如果
Foo
子类构造函数]。)
If I do
Foo.newProp = "new addition"
why isf.newProp => undefined
?
Foo.new = ...
更改为
Foo.newProp = ...
,因为
new
是一个关键字。虽然您可以像在 ES5 中那样使用它,但最好不要这样做。)
Foo.newProp
与
f
几乎没有任何关系.您可以在
f.constructor.newProp
上找到它, 自
f.constructor
是
Foo
.
function Foo() {
}
Foo.prototype.bar = "I'm bar on Foo.prototype";
Foo.bar = "I'm bar on Foo";
+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | V +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ +−−>| [String] | || Foo [Function] | | +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ | | "I'm bar on Foo" | || bar |−−−−+ +−−−−−−−−−−−−−−−−−−+ || prototype |−−−−+ |+−−−−−−−−−−−−−−−−+ | | +−−−−−−−−−−+ | | | V | +−−−−−−−−−−−−−+ | | [Object] | | +−−−−−−−−−−−−−+ | | constructor |−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | bar |−−−−−>| [String] | +−−−−−−−−−−−−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | "I'm bar on Foo.prototype" | +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
Now if we do
var f = new Foo();
+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | V +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ +−−>| [String] | || Foo [Function] | | +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ | | "I'm bar on Foo" | || bar |−−−−+ +−−−−−−−−−−−−−−−−−−+ || prototype |−−−−+ |+−−−−−−−−−−−−−−−−+ | | +−−−−−−−−−−−−−+ | | | V |+−−−−−−−−−−−−−−−+ +−−−−−−−−−−−−−+ || f [Object] | +−−−−−>| [Object] | |+−−−−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+ || [[Prototype]] |−−−−−−−−−−+ | constructor |−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−++−−−−−−−−−−−−−−−+ | bar |−−−−>| [String] | +−−−−−−−−−−−−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | "I'm bar on Foo.prototype" | +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
([[Prototype]] is an object's internal field referring to its prototype. This is accessible via Object.getPrototypeOf
[or __proto__
on JavaScript engines on web browsers, but don't use __proto__
, it's just for backward compatibility with old SpiderMonkey-specific code.)
Now suppose we do this:
f.charlie = "I'm charlie on f";
f
对象(
粗体 中的新内容):
+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | V +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ +−−>| [String] | || Foo [Function] | | +−−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−+ | | "I'm bar on Foo" | || bar |−−−−+ +−−−−−−−−−−−−−−−−−−+ || prototype |−−−−+ |+−−−−−−−−−−−−−−−−+ | | +−−−−−−−−−−−−−+ | | | V |+−−−−−−−−−−−−−−−+ +−−−−−−−−−−−−−+ || f [Object] | +−−−−−>| [Object] | |+−−−−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+ || [[Prototype]] |−−−−−−−−−−+ | constructor |−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+| charlie |−−−−−−−−−−+ | bar |−−−−−>| [String] |+−−−−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+ +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | "I'm bar on Foo.prototype" | | +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | +−−−−−−−−−−−−−−−−−−−−+ +−−−−−>| [String] | +−−−−−−−−−−−−−−−−−−−−+ | "I'm charlie on f" | +−−−−−−−−−−−−−−−−−−−−+
f
now has its own property, called charlie
. This means that these two statements:
console.log(f.charlie); // "I'm charlie on f"
console.log(f.bar); // "I'm bar on Foo.prototype"
f.charlie
第一的。这是引擎如何处理
f.charlie
:
f
有自己的属性叫做 "charlie"
? f.bar
:
f
有自己的属性叫做 "bar"
? f
有原型(prototype)吗? f
的原型(prototype)有一个名为 "bar"
的属性? f.charlie
之间有很大的区别和
f.bar
:
f
有它的
自己的 名为
charlie
的属性,但是一个
继承名为
bar
的属性.如果
f
的原型(prototype)对象没有名为
bar
的属性,它的原型(prototype)对象(在这种情况下,
Object.prototype
)将被检查,依此类推,直到我们用完原型(prototype)。
hasOwnProperty
测试一个属性是否是“自己的”属性。所有对象都具有的功能:
console.log(f.hasOwnProperty("charlie")); // true
console.log(f.hasOwnProperty("bar")); // false
I make
function Person(first_name, last_name) {this.first_name = first_name; this.last_name = last_name;}
and thenvar ilya = new Person('ilya', 'D')
how does it resolves the innername
properties?
Person
这是
new Person(...)
的一部分表达式,
this
new
返回的新生成的对象表达。所以当你做
this.prop = "value";
,您直接在该对象上放置一个属性,与原型(prototype)无关。
p
目的:
// Example 1:
function Person(name) {
this.name = name;
}
var p = new Person("Fred");
// Example 2:
function Person() {
}
var p = new Person();
p.name = "Fred";
function clog(x){
var text = x;
return console.log(text ); // <=== here
}
new
操作工程是:
prototype
中获得了一个原型(prototype)。属性(property)。 this
指向新对象。 new
的结果expression 是在步骤 1 中创建的对象。new
操作是那个对象。 console.log
不返回任何内容,您只需删除
return
代码中的关键字。但是如果你使用了
return xyz();
用一个返回对象的函数构造,你会弄乱你的构造函数。
clog.prototype.alert = alert(text);
alert
函数并将其结果分配给名为
alert
的属性在
clog.prototype
.自
alert
不返回任何内容,它完全等同于:
alert(text);
clog.prototype.alert = undefined;
clog.prototype.alert = function(text) {
alert(text);
};
alert
原型(prototype)上的属性。当函数被调用时,它会调用标准
alert
.
new
的函数)应该以大写字母开头,所以
Clog
而不是
clog
.不过,这只是风格。
关于javascript - 向没有 .prototype 的构造函数添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582341/
以下代码,我使用 chrome 浏览器控制台进行了检查: function A(){ this.a='a' } 这是一个构造函数。我已经将一个属性 b 赋给了 A 的原型(prototype)。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 5年前关闭。 Improve this
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 3年前关闭。 Improve this
我已经开始阅读 The Pragmatic Programmer,我很喜欢并学习堆形式,但我很难理解示踪子弹和原型(prototype)之间的区别。跟踪项目符号是否像拥有应用程序的所有 View 但尚
尽管阅读了 StackOverflow 上的大多数文章,但我现在实际上对原型(prototype)非常困惑。 function Foo() { } Foo.prototype.speak = func
我正在阅读以下代码,并开始想知道 Rectangle.prototype = Object.create(Shape.prototype) 和 Rectangle.prototype = Shape.
我想知道它们之间的区别: childObj.prototype = Object.create(parentObj.prototype) 和 childObj.prototype = parentOb
这个问题在这里已经有了答案: Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype =
在 node.js 中导出原型(prototype)的首选方法是什么?您可以采用两种方法: 导出原型(prototype)本身 function A () { } module.exports = A
我正在学习 JavaScript,发现了两种分配原型(prototype)的方法。 第一个是A.prototype = B.prototype,第二个是A.prototype = new B() 例如
在一些构造函数的定义之后,例如 child ,我见过以下两种形式: Child.prototype = Parent.prototype; 或 Child.prototype = new Parent
我正在阅读一本关于 OOP javascript 的书,但被其中一个示例卡住了。 在示例代码的第一个版本中,Shape 的一个新实例构造函数被创建并且 toString方法被调用。 toString方
这个问题在这里已经有了答案: What should I connect to the child prototype property in JavaScript (2 个答案) 关闭 8 年前。
在进行原型(prototype)设计时,您在多大程度上放弃了最佳实践来支持代码和修复黑客攻击?当然,代码并不打算在完整的生产环境中保留。 补充:我正在研究一个用 Python 制作的相当大的半工作原型
我正在尝试使用 Prototype 更新隐藏表单字段的值。表单域: 我正在尝试使用原型(prototype)更新值: var additionalVal = ',2'; var itemId = $
我正在阅读How to Make a Javascript Library我发现了作者所说的一个观点: function _() { //Some obects and var
我想用一个新函数扩展“Number”类型,因此我必须定义一个原型(prototype)。当我想到这一点时,我得到了一堆问题: Number 是否既继承了 Object.prototype 又继承了 F
这里好像有区别... 假设我们有 function MyConstructor() {} MyConstructor 的[[Prototype]] 是Function.prototype,不是 MyC
有人建议 Derived.prototype = Object.create(Base.prototype); 优于 Derived.prototype = new Base(); (如 this S
我是一名优秀的程序员,十分优秀!