Prototype-based programming

Posted zzfx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Prototype-based programming相关的知识,希望对你有一定的参考价值。

Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects via delegation that serve as prototypes. This model can also be known as prototypalprototype-oriented, classless, or instance-based programming. Delegation is the language feature that supports prototype-based programming.

Prototype object oriented programming uses generalized objects, which can then be cloned and extended. Using fruit as an example, a "fruit" object would represent the properties and functionality of fruit in general. A "banana" object would be cloned from the "fruit" object, and would also be extended to include general properties specific to bananas. Each individual "banana" object would be cloned from the generic "banana" object. Compare to the class-based paradigm, where a "fruit" class (not object) would be extended by a "banana" class.

 

// Example of true prototypal inheritance style 
// in javascript.

// object creation using the literal 
// object notation {}.
var foo = {name: "foo", one: 1, two: 2};

// Another object.
var bar = {two: "two", three: 3};

// Object.setPrototypeOf() is a method introduced in ECMAScript 2015.
// For the sake of simplicity, let us pretend 
// that the following line works regardless of the 
// engine used:
Object.setPrototypeOf(bar, foo); // foo is now the prototype of bar.

// If we try to access foo‘s properties from bar 
// from now on, we‘ll succeed. 
bar.one // Resolves to 1.

// The child object‘s properties are also accessible.
bar.three // Resolves to 3.

// Own properties shadow prototype properties
bar.two; // Resolves to "two"
bar.name; // unaffected, resolves to "foo"
foo.name; // Resolves to "foo"

This example in JS 1.8.5+ (see https://kangax.github.com/es5-compat-table/)

var foo = {one: 1, two: 2};

// bar.[[prototype]] = foo
var bar = Object.create(foo);

bar.three = 3;

bar.one; // 1
bar.two; // 2
bar.three; // 3

在使用委托的基于原型的语言中,运行时语言可以仅仅通过循着一个序列的指针直到找到匹配这样的方式来定位属性或者寻找寻找正确的数据。所有这些建立行为共享的行为 需要的是委托指针。
不像是基于类的面向对象语言中类和借口的关系,原型和他的分支之间的关系并不要求子对象有相似的内存结构,因为如此,子对象可以继续修改和...而无须像基于类的系统那样整理结构。
还有一个要提到的地方是,不仅仅是数据,方法也能被修改。因为这个原因,大多数基于类型的语言把数据和方法提作“slots”。

 

https://en.wikipedia.org/wiki/Prototype-based_programming






以上是关于Prototype-based programming的主要内容,如果未能解决你的问题,请参考以下文章

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

programming terms

GPU学习随笔

C200 Programming

CAB402 Programming