简体   繁体   English

Javascript中基于函数原型的编程的语义

[英]Semantics of functional prototype-based programming in Javascript

Let's say we inherit 90% of code functionality from a boilerplate prototype-based funciton: 假设我们从基于样板原型的功能继承了90%的代码功能:

var BoilerplateForOtherFunctions = function(){};

BoilerplateForOtherFunctions.prototype.addOneYear = function(){
  return this.myAge += 1;
};

BoilerplateForOtherFunctions.prototype.getAge = function(myUrl){
  return this.myAge;
};



And we have many functions that inherits this prototype and used it like this: 我们有许多函数可以继承此原型并像这样使用它:

var MyNormalFnOne = function(){
  this.myAge = "34";
  console.log( this.getAge() );
  this.addOneYear();
};

// only injects the boilerplate object into our function
underscore.extend(MyNormalFnOne.prototype, BoilerplateForOtherFunctions.prototype);

new MyNormalFnOne();



Everything works well. 一切正常。

Problem is - that we rely on a fact, that someone has defined this.myAge before we use anything from the boilerplate prototype. 问题是-我们依赖一个事实,即有人使用样板原型中的任何内容之前已经定义了this.myAge This is very common solution of programming in Javascript. 这是使用Java编程的非常常见的解决方案。

Wouldn't be better to pass all arguments into the prototype fn and be more functional? 将所有参数传递给原型fn并使其更具功能性会更好吗? It would lead to faster debugging, allows function cashing, creates less coding mistakes and program errors. 这将导致更快的调试,允许函数兑现,减少编码错误和程序错误。



Into something like this (more functional approach): 变成这样(更实用的方法):

var BoilerplateForOtherFunctions = function(){};

BoilerplateForOtherFunctions.prototype.addOneYear = function(age){
  return age += 1;
};

BoilerplateForOtherFunctions.prototype.getAge = function(age){
  return age; // makes no sense, here - just a sample, of course
};


var MyNormalFnOne = function(){
  var myAge = "34";
  console.log( this.getAge(myAge) );
  myAge = this.addOneYear(myAge);
};

// only injects the boilerplate object into our function
underscore.extend(MyNormalFnOne.prototype, BoilerplateForOtherFunctions.prototype);

new MyNormalFnOne();



The problem with second appoarch is (with a functional one) 第二个审批人的问题是(有职能的)
- passing many arguments to every function -向每个函数传递许多参数
- return value must be assigned again, it doesn't "magically" work -必须再次分配返回值,这不能“神奇地”工作

The problem with first appoarch is (with a traditionals) 第一次审批的问题是(传统上)
- difficult debugging, not reliable code, no fn cashing, ... -困难的调试,不可靠的代码,没有fn兑现,...




Any opinion? 有什么意见吗?

The constructor function is there to initialize instance specific members and the prototype is for shared members (like functionality). 构造函数用于初始化实例特定的成员,原型用于共享成员(如功能)。

Since your prototype assumes myAge to exist you should set it in BoilerplateForOtherFunctions and re use it in types that inherit from it: 由于您的原型假定存在myAge,因此应在BoilerplateForOtherFunctions中对其进行设置,并在从其继承的类型中重新使用它:

var BoilerplateForOtherFunctions = function(args){
  args=args||{};
  this.myAge = args.myAge === 0? 0 ://either 0
     args.myAge || 10;//or 10 when not set
};

BoilerplateForOtherFunctions.prototype.addOneYear = function(){
  return this.myAge += 1;
};

BoilerplateForOtherFunctions.prototype.getAge = function(myUrl){
  return this.myAge;
};

var MyNormalFnOne = function(args){
  //re using parent constructor
  BoilerplateForOtherFunctions.call(this,args);
  console.log( this.getAge() );
  this.addOneYear();
};

// use Object.create will have instances of MyNormalFnOne be an instanceof BoilerplateForOtherFunctions
MyNormalFnOne.prototype = Object.create(BoilerplateForOtherFunctions.prototype);
//repair prototype.constructor after assigning a new value to prototype
MyNormalFnOne.prototype.constructor = MyNormalFnOne;

var oneFirst = new MyNormalFnOne();
var oneSecond = new MyNormalFnOne({myAge:22});

More info on constructor functions and prototype here: Prototypical inheritance - writing up 有关构造函数和原型的更多信息,请参见: 原型继承-编写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 限制了基于原型的继承? - JavaScript limits to prototype-based inheritance? JavaScript基于原型的继承的好例子 - Good Example of JavaScript's Prototype-Based Inheritance 使用打字稿是否会基本放弃基于原型的编程范例? - Does using typescript basically give up the prototype-based programming paradigm? 函数声明中指定的基于原型的继承 - Prototype-based inheritance specified in the function declaration ECMAScript 2015中引入的JavaScript类与JavaScript现有的基于原型的继承之间有区别吗? - Difference between JavaScript classes introduced in ECMAScript 2015 and JavaScript's existing prototype-based inheritance? 较新的 JavaScript (ES6) 和 TypeScript 中原型/基于原型的继承的最佳实践? - Best practices for prototypal / prototype-based inheritance in newer JavaScript (ES6) & TypeScript? JavaScript 中基于原型的类语法和类语法有什么区别? - What is the difference between prototype-based class syntax and class syntax in JavaScript? 发送带有$ .ajax的基于原型的类时,有“ this”的问题 - Trouble with `this` when sending prototype-based class with $.ajax 是否有整个开发周期的基于原型的语言? - Are there any prototype-based languages with a whole development cycle? 基于轻型原型的浮动/可调整大小的弹出窗口 - Lightweight Prototype-based floating/resizable popup window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM