溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

ruby與javascript開(kāi)發(fā)的簡(jiǎn)單比較

發(fā)布時(shí)間:2020-06-24 02:26:46 來(lái)源:網(wǎng)絡(luò) 閱讀:702 作者:oulafen 欄目:編程語(yǔ)言

前言


       之前用javascript做了可以實(shí)現(xiàn) 報(bào)名-競(jìng)價(jià) 活動(dòng)的party bid,如今用ruby語(yǔ)言為party_bid做了個(gè)服務(wù)端,可以創(chuàng)建用戶(hù),每個(gè)用戶(hù)都可以組織自己的party_bid活動(dòng),并且設(shè)置的管理員可以看到所有用戶(hù)信息,并對(duì)這些用戶(hù)信息進(jìn)行管理。

   這前后分別學(xué)習(xí)了js與ruby,對(duì)它們有了初步的認(rèn)識(shí),現(xiàn)在對(duì)它們之間的共性與不同做個(gè)簡(jiǎn)單的總結(jié)。


正文


1. 大概簡(jiǎn)介

Ruby:Ruby 是一種面向?qū)ο?、命令式、函?shù)式、動(dòng)態(tài)的通用編程語(yǔ)言,其設(shè)計(jì)理念是人性化,程序員可以不用寫(xiě)很多代碼就能實(shí)現(xiàn)想要的功能,多用于后臺(tái)服務(wù)端。

Javascript:JavaScript 是一種廣泛應(yīng)用于客戶(hù)端網(wǎng)頁(yè)(瀏覽器)開(kāi)發(fā)的腳本語(yǔ)言,可以給 HTML 網(wǎng)頁(yè)添加動(dòng)態(tài)功能,響應(yīng)用戶(hù)的各種操作,被廣泛用于客戶(hù)端。

2. 正式PK

Array:
map方法使用:


Ruby:
a = ["1", "2"]
a.push("3")
a.map!(&:to_i)    # [1, 2, 3]


JS:
var a = ["1", "2"];
a.push("3");
a = a.map(function(n) { return parseInt(n, 10); });



遍歷方法的使用:

Ruby:
a = [1, 2, 3]
a.each {|n| puts n}
#or
a.each do |n|
    puts n
end
#or
for i in 0..(a.length - 1) do
  puts a[i]
end


JS:
var a = [1, 2, 3];
a.forEach(function(n) { console.log(n); })
//or
for (var i = 0; i < a.length; i++) {
  console.log(a[i]);
}


Strings:

獲取字符串的索引:

Ruby:
'hello'.index('e')    # 1
'hello'.rindex('l')   # 3
JS:
'hello'.indexOf('e')             // 1
'hello'.lastIndexOf('l')         // 3


判斷字符串的值:

Ruby:
if 'hello'.include? 'lo'
    puts 'found'
end
JS:


if (~'hello'.indexOf('lo')) {
  console.log('found');
}



新建字符串:

Ruby:
'hello' * 3   # 'hellohellohello'
JS:
(new Array(3 + 1)).join('hello')
    // 'hellohellohello'


Hash:

定義(方法一樣):

Ruby:
h = {}
h['a'] = 1
h['b'] = 2
JS:
var h = {};
h['a'] = 1;
h['b'] = 2;  //以下兩個(gè)用例中的h即為此處定義的h

遍歷:


Ruby:
h.each {|key, value| puts "#{key} #{value}" }
JS:
for (key in h) { console.log(key, h[key]); }


取屬性:

Ruby:
h.keys # ['a', 'b']
h.has_key?('c') # false
JS:
Object.keys(h); // ['a', 'b']
h.hasOwnProperty('c') // false

取長(zhǎng)度:

Ruby:
h.length    # 2
JS:
Object.keys(h).length    // 2

刪除:

Ruby:
h.delete("b")
JS:
delete h.b
Functions:

定義與調(diào)用:

Ruby:
#定義
def plus_5(num = 0) num + 5 end 
#調(diào)用
plus_5     # 5
plus_5(10) # 15
[5, 10, 15].map { |k| plus_5(k) } # [10, 15, 20]
JS:
//定義
function plus_5(num) { return (num || 0) + 5; }
//調(diào)用
plus_5();   // 5
plus_5(10); // 15
[5, 10, 15].map(plus_5);   // [10, 15, 20]
Math:
Ruby:
[-5, -1, -8].max            # -1
[-5, 15, 20].reduce(0, &:+) # 30
JS:
Math.max.apply(null, [-5, -1, -8])   // -1
[-5, 15, 20].reduce(function(sum, value) {
         return sum + value;
    }, 0)      // 30
Random:
Ruby:
prng = Random.new()
prng.rand(5..9)   # one of [5, 6, 7, 8, 9]
JS:
function rand(a, b) {
  return Math.floor(Math.random() * (b-a+1)+a);
 }
rand(5, 9);    // one of [5, 6, 7, 8, 9]



總結(jié)


       總的簡(jiǎn)單來(lái)說(shuō),與javascript相比,ruby是純面向?qū)ο笳Z(yǔ)言,對(duì)局部變量的定義不用加前綴var,ruby對(duì)函數(shù)的定義不用function打頭,而是def,在方法中不用括號(hào),返回值直接寫(xiě)變量名,不用return,并且ruby代碼中沒(méi)有標(biāo)點(diǎn),看上去更加干凈整潔。


參考http://agentcooper.github.io/js-ruby-comparison/

                                                                                                 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI