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 prototypal, prototype-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.
Almost all prototype-based systems are based on interpreted and dynamically typed languages.
Object construction[edit]
In prototype-based languages there are no explicit classes. Objects inherit directly from other objects through a prototype property. The prototype property is called prototype in Self and JavaScript, or proto in Io. There are two methods of constructing new objects: ex nihilo ("from nothing") object creation or through cloning an existing object. The former is supported through some form of object literal, declarations where objects can be defined at runtime through special syntax such as {...} and passed directly to a variable. While most systems support a variety of cloning, ex nihilo object creation is not as prominent.[4]
In class-based languages, a new instance is constructed through a class's constructor function, a special function that reserves a block of memory for the object's members (properties and methods) and returns a reference to that block. An optional set of constructor arguments can be passed to the function and are usually held in properties. The resulting instance will inherit all the methods and properties that were defined in the class, which acts as a kind of template from which similar typed objects can be constructed.
Delegation[edit]
In prototype-based languages that use delegation, the language runtime is capable of dispatching the correct method or finding the right piece of data simply by following a series of delegation pointers (from object to its prototype) until a match is found. All that is required to establish this behavior-sharing between objects is the delegation pointer. Unlike the relationship between class and instance in class-based object-oriented languages, the relationship between the prototype and its offshoots does not require that the child object have a memory or structural similarity to the prototype beyond this link. As such, the child object can continue to be modified and amended over time without rearranging the structure of its associated prototype as in class-based systems. It is also important to note that not only data, but also methods can be added or changed. For this reason, some prototype-based languages refer to both data and methods as "slots" or "members".[citation needed]
// 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的更多相关文章
- Prototype based langue LUA
Prototype-based programming https://en.wikipedia.org/wiki/Prototype-based_programming Prototype-base ...
- javascript 之 prototype 浅析
prototype 原型 javascript 是一种 prototype based programming 的语言, 而与我们通常的 class based programming 有很大 的区别 ...
- javascript必知必会之prototype
本博客所有内容采用 Creative Commons Licenses 许可使用. 引用本内容时,请保留 朱涛, 出处 ,并且 非商业 . 点击 RSS 进行订阅.(推荐使用 google reade ...
- JavaScript prototype原型链介绍
javascript 是一种基于原型的编程 (prototype based programming) 的语言, 而与我们通常的基于类编程 (class based programming) 有很大的 ...
- Atitit.prototype-base class-based 基于“类” vs 基于“原型”
Atitit.prototype-base class-based 基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2. 对象的产生有两种基本方式.一种是以原型(proto ...
- Questions that are independent of programming language. These questions are typically more abstract than other categories.
Questions that are independent of programming language. These questions are typically more abstract ...
- Java 面向切面编程(Aspect Oriented Programming,AOP)
本文内容 实例 引入 原始方法 装饰者模式 JDK 动态代理和 cglib 代理 直接使用 AOP 框架--AspectWerkz 最近跳槽了,新公司使用了 AOP 相关的技术,于是查点资料,复习一下 ...
- 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA
1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...
- Dynamic dispatch
Dynamic dispatch动态调度.动态分发 In computer science, dynamic dispatch is the process of selecting which im ...
- Dynamic dispatch mechanisms
Normally, in a typed language, the dispatch mechanism will be performed based on the type of the arg ...
随机推荐
- 文字纵向滚动marquee
<div style="width:200px; height:300px"><marquee direction="up" truespee ...
- Java中类的定义
成员变量:对应事物的属性 成员方法:对应事物的行为 类定义的格式 定义类:就是定义类的成员,包括成员变量和成员方法 成员变量:和以前定义变量几乎是一样的.只不过位置发生了改变.在类中,方法外. 成员方 ...
- Python笔记21-------浅复制和深复制、赋值
上面图表示浅复制和深复制,针对顶层对象来说,赋值为引用,浅复制和深复制都是复制一个新的对象. 针对子对象来说,浅复制为引用.深复制就是复制两个一样的. 1.赋值 A= [ 1,2,3, [ 'a',' ...
- 网络教程(9)ARP。IP和以太网间映射
question: how does a knows that SFO is the right place to send as Ethernet frame subnet Mask: its ju ...
- 【JavaScript框架封装】在实现一个自己定义类似于JQuery的append()函数的时候遇到的问题及解决方案
主要问题: 在刚开始创建了这个函数之后,使用的时候,总是会出现一个问题,就是按照正常步骤给一个ID选择器添加子节点的时候正常,但是到了给一个class选择器的元素添加的时候始终只能添加一个. 下面是我 ...
- Codeforces 789A Anastasia and pebbles( 水 )
链接:传送门 题意:这个人每次都去公园捡石子,她有两个口袋,每个口袋最多装 k 个石子,公园有 n 种石子,每种石子 w[i] 个,询问最少几天能将石子全部捡完 思路:排个序,尽量每天都多装,如果 k ...
- LVS的使用
lvs: Linux Virtual Server l4:四层交换:四层路由: 根据请求报文的目标IP和PORT将其转发至后端主机集群中的某一台主机(根据挑选算法): netfilter: PRERO ...
- apache https部署
1.生成证书,直接在阿里云或腾讯云中生成此处不再介绍 2.在httpd.conf中取消#LoadModule ssl_module modules/mod_ssl.so的注释 3.开启httpd-ss ...
- 02.OOP面向对象-3.一些理解
对封装的理解? 封装,类本身就是一个封装,封装了属性和方法.方法也是封装,对一些业务逻辑的封装.私有也是封装,将一些方法和属性私有化,对外提供可访问的接口. 对继承的理解 将共性的内容放在父类中,子类 ...
- Problem 13
Problem 13 # Problem_13 """ Work out the first ten digits of the sum of the following ...