1 /*
  2  * Filename: Functor.js
  3  * Created By: Ranando D. King
  4  * License: Apache 2.0
  5  *
  6  * Copyright 2014 Ranando D. King
  7  *
  8  * Licensed under the Apache License, Version 2.0 (the "License");
  9  * you may not use this file except in compliance with the License.
 10  * You may obtain a copy of the License at
 11  *
 12  * 		http://www.apache.org/licenses/LICENSE-2.0
 13  *
 14  * Unless required by applicable law or agreed to in writing, software
 15  * distributed under the License is distributed on an "AS IS" BASIS,
 16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17  * See the License for the specific language governing permissions and
 18  * limitations under the License.
 19  */
 20 var Functor = (function() {
 21     var $$ = function Functor(obj, method, unsealed) {
 22         var isFixed = false;
 23         var retval = function functorCall() {
 24             return method.apply(obj, arguments);
 25         };
 26 
 27         Object.defineProperties(retval, {
 28             "_this": {
 29                 get: function getThis() { return obj; },
 30                 set: function setThis(val) { !isFixed && (obj = val); }
 31             },
 32             "_method": {
 33                 get: function getMethod() { return method; },
 34                 set: function setMethod(val) { !isFixed && (method = val); }
 35             },
 36             isFunctor: {
 37                 value: true
 38             },
 39             /*apply: {
 40                 enumerable: true,
 41                 value: function apply(owner, params) {
 42                     if (owner === undefined)
 43                         owner = obj;
 44 
 45                     method.apply(owner, params);
 46                 }
 47             },*/
 48             rescope: {
 49                 value: function rescope(newObj) {
 50                     return new Functor(newObj, method);
 51                 }
 52             },
 53             fix: {
 54                 value: function fix() {
 55                     isFixed = true;
 56                     Object.freeze(this);
 57                 }
 58             }
 59         });
 60 
 61         if (!unsealed)
 62             Object.seal(retval);
 63         return retval;
 64     };
 65 
 66     Object.seal($$);
 67     return $$;
 68 })();
 69 
 70 //If some form of require.js exists, export the class
 71 if (module && exports && module.exports === exports)
 72     module.exports = Functor;
 73