• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

응용해서 만들어봤는데 신기한걸 발견했습니다.

19.05.28 11:59 작성 조회수 92

0


<body>
<div id='app'>

<h1>소모품대장 계산 도우미</h1>

<div class="inputBox">
<small>현재 재고는 <b>{{ inStock }}</b>입니다.</small>
<input class="use" type="text" placeholder="불출량 입력하세요" v-model="use">
<button v-on:click="usingInStock(use)" v-on:keyup.enter="usingInStock(use)">사용</button>
</div>
<div class="inputBox">
<small>추가 입고분이 있다면 입력해주세요.</small>
<input class="store" type="text" placeholder="입고량 입력하세요" v-model="store">
<button v-on:click="storingInStock(store)" v-on:keyup.enter="storingInStock(store)">사용</button>
</div>

<ul class="todoList">
<li v-for="(record, index) in records">
<span>{{index}} :: 날짜는 : {{record.Today_date}} - 재고는 : {{ record.currunt_data }} </span>
</li>
</ul>

</div>
<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>

<script>
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var vm = new Vue({
el: '#app',
data: {
inStock: '1581',
use: '',
store: '',
str1: '',
tempDate: new Date(2018, 0, 1),
fistDate: '',
records: [
{Today_date: '', currunt_data: ''}
],
},
methods: {
usingInStock: function(use){
this.inStock -= this.use
var d = new Date();
var week = new Array('일', '월', '화', '수', '목', '금', '토');
// this.tempDate.setDate(this.tempDate.getDate() + getRandomIntInclusive(6, 7))
this.tempDate.setDate(this.tempDate.getDate() + 7)
// this.str1 = week[this.tempDate.getDay()]
// console.log("하하하", this.str1, d.getDate(), d.getDay())
this.records.unshift({Today_date:this.tempDate.toLocaleString('ko-KR', {year: "numeric", month:"2-digit", day:"2-digit", weekday: 'short' }), currunt_data: this.inStock})
 
},
storingInStock: function(store){
this.store = parseInt(this.store)
this.inStock = parseInt(this.inStock)
this.inStock += this.store
}
}
}
)
</script>

</body>

위와 같이 코드를 만들어봤는데요

this.inStock에서 -= 변수 하면 instock이 int형으로 잘 반응합니다. *= 도 잘 반응합니다.

근데 왜 +=하면 str + str로 되버려서 

제가 따로 저렇게 형변환을 해줘야됩니다. 

왜이런지 궁금합니다.

답변 1

답변을 작성해보세요.

1

자바스크립트 문법에서, 숫자형과 문자열의 연산은 다음과 같습니다.

a + b 는, String Concatenation 을 고려하여, 숫자형을 문자형으로 변환해서 처리 합니다.

그 외, a - b, a * b, a / b 는, 문자형을 숫자형으로 변환해서 처리합니다.

좀 복잡한 면이 있으므로, 자바스크립트 연산자 문법을 한번 보시기 바랍니다.