IF function, but in French

I would like to code a function called SI, which works as if (SI is the French word if), but with this syntax:

SI(condition)
ALORS{
function hello()
}
SINON{
function bye();
function goodbye();
}

In Javascript, this will be:

if(condition){
    function hello();
}
else{
function bye();
function goodbye();
}

Is it possible?

Thank!

+4
source share
2 answers

Try it,

function si(condition) {
    return {
        alors: function(callback) {
            if (condition) callback();
            return this;
        },
        sinon: function(callback) {
            if (!condition) callback();
            return this;
        }
    };
}

var x = 0;

si(x == 1)
  .alors(() => console.log("Bonjour!"))
  .sinon(() => console.log("Au revoir!"));

How it works?

  • si is a function that returns an object
  • This object has two functions: alorsandsinon
  • alorsis a function that takes another function as an argument callback, if condition(which was passed to si) is true, then it alorswill execute a callback function
  • () => console.log(...)is a short notation for function() { return console.log(...); }which creates an anonymous function.
+5
source

: , js. :

function si(condition,alors,sinon){
  if(condition){
   alors();
  }else{
   sinon();
 }
 }

:

si(a==true,
 //alors
 function(){
  alert("a");
 },
 //sinon
 function(){
 alert("no a");
 });

:

function evaluate(obj){
 if(obj.si){
   obj.alors();
 }else{
   obj.sinon();
 };

:

evaluate({
si:a==true,
alors:function(){},
sinon:function(){}
});

native js, :

function frenchjs(string){
var translate={
si:"if",
sinone:"else"
};
for(key in translate){
string=string.replace(key,translate[key]);
}
eval(string);
}

:

frenchjs("si(a==true){alert('a')}sinnon{alert('no a');}");
+1

Source: https://habr.com/ru/post/1665306/


All Articles