Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用变量时未在代码中初始化_Javascript - Fatal编程技术网

Javascript 使用变量时未在代码中初始化

Javascript 使用变量时未在代码中初始化,javascript,Javascript,我正在做一些事情,我遇到了一些正在使用但没有初始化的变量类型,如果有人能详细解释的话 这是代码,我在谈论notesObj和它是如何生成的,还有为什么if(notes==null){notesObj=[];}或者{notesObj=JSON.parse(notes);在所有函数上都被使用,不能只启用吗?在所有函数上,我的意思是我们可以减少它吗 //Show hide Notes const notes_btn = document.querySelector('#notes-btn'); cons

我正在做一些事情,我遇到了一些正在使用但没有初始化的变量类型,如果有人能详细解释的话

这是代码,我在谈论notesObj和它是如何生成的,还有为什么
if(notes==null){notesObj=[];}或者{notesObj=JSON.parse(notes);
在所有函数上都被使用,不能只启用吗?在所有函数上,我的意思是我们可以减少它吗

//Show hide Notes
const notes_btn = document.querySelector('#notes-btn');
const notes_container = document.querySelector('.container');

notes_btn.addEventListener('click', () => {
    if (!notes_container.contains('show')) {
        notes_container.add('show')
    } else notes_container.remove('show');
})

// Add note to local storage
const addBtn = document.getElementById("add-btn");
var addTitle = document.getElementById("note-title");
var addTxt = document.getElementById("note-text");
addBtn.addEventListener("click", () => {
    if (addTitle.value == "" || addTxt.value == "") {
        return alert("Permbajtja e Shenimit dhe titulli jan boshe.")
    }
    let notes = localStorage.getItem("notes");
    if (notes == null) {
        notesObj = [];
    } else {
        notesObj = JSON.parse(notes);
    }
    let myObj = {
        title: addTitle.value,
        text: addTxt.value
    }
    notesObj.push(myObj);
    localStorage.setItem("notes", JSON.stringify(notesObj));
    addTxt.value = "";
    addTitle.value = "";
    showNotes();
});

// Function to show elements from localStorage
function showNotes() {
    let notes = localStorage.getItem("notes");
    if (notes == null) {
        notesObj = [];
    } else {
        notesObj = JSON.parse(notes);
    }
    let content = "";
    notesObj.forEach((element, index) => {
        content += `
        <div class="note">
            <h3 class="note-title"> ${element.title} </h3>
            <button id="${index}" onclick="deleteNote(this.id)" class="note-btn">Fshije</button>
            <button id="${index}" onclick="editNote(this.id)" class="note-btn edit-btn">Shikoje</button>
        </div>
            `;
    });
    let notesElm = document.getElementById("notes");
    if (notesObj.length != 0) {
        notesElm.innerHTML = content;
    } else {
        notesElm.innerHTML = `Nuk ka shenime, shtoni nje !`;
    }
}

// Function to delete a note
function deleteNote(index) {
    let confirmDel = confirm("A jeni te sigurt qe deshironi ta fshini Shenimin tuaj?");
    if (confirmDel == true) {
        let notes = localStorage.getItem("notes");
        if (notes == null) {
            notesObj = [];
        } else {
            notesObj = JSON.parse(notes);
        }
        notesObj.splice(index, 1);
        localStorage.setItem("notes", JSON.stringify(notesObj));
        showNotes();
    }
}

// Function to Edit the Note
function editNote(index) {
    let notes = localStorage.getItem("notes");

    if (addTitle.value !== "" || addTxt.value !== "") {
        return alert("Ju lutem fshini te dhenat nga forma per ta redaktuar nje shenim.")
    }

    if (notes == null) {
        notesObj = [];
    } else {
        notesObj = JSON.parse(notes);
    }

    notesObj.findIndex((element, index) => {
        addTitle.value = element.title;
        addTxt.value = element.text;
    })
    notesObj.splice(index, 1);
    localStorage.setItem("notes", JSON.stringify(notesObj));
    showNotes();
}

showNotes();
//显示隐藏注释
const notes_btn=document.querySelector(“#notes btn”);
const notes_container=document.querySelector(“.container”);
注释\u btn.addEventListener('单击',()=>{
如果(!notes_container.contains('show')){
注释\u容器。添加('show')
}else notes_容器。移除('show');
})
//将注释添加到本地存储
const addBtn=document.getElementById(“add btn”);
var addTitle=document.getElementById(“注释标题”);
var addTxt=document.getElementById(“注释文本”);
addBtn.addEventListener(“单击”,()=>{
如果(addTitle.value==“”| | addTxt.value==“”){
返回警报(“Permbajja e Shenimit dhe titulli jan boshe.”)
}
让notes=localStorage.getItem(“notes”);
if(notes==null){
注sobj=[];
}否则{
notesObj=JSON.parse(notes);
}
设myObj={
标题:addTitle.value,
文本:addTxt.value
}
注:推送(myObj);
setItem(“notes”,JSON.stringify(notesObj));
addTxt.value=“”;
addTitle.value=“”;
showNotes();
});
//函数显示本地存储中的元素
函数showNotes(){
让notes=localStorage.getItem(“notes”);
if(notes==null){
注sobj=[];
}否则{
notesObj=JSON.parse(notes);
}
让内容=”;
notesObj.forEach((元素,索引)=>{
内容+=`
${element.title}
菲舍耶
Shikoje
`;
});
让notesElm=document.getElementById(“notes”);
如果(注:长度!=0){
notesElm.innerHTML=内容;
}否则{
notesElm.innerHTML=`Nuk ka shenime,shtoni nje!`;
}
}
//函数删除注释
函数删除注释(索引){
让confirmDel=确认(“一个杰尼·德希罗尼·塔夫希尼·谢尼明·塔吉?”;
如果(confirmDel==true){
让notes=localStorage.getItem(“notes”);
if(notes==null){
注sobj=[];
}否则{
notesObj=JSON.parse(notes);
}
注:接头(索引1);
setItem(“notes”,JSON.stringify(notesObj));
showNotes();
}
}
//函数编辑注释
功能编辑说明(索引){
让notes=localStorage.getItem(“notes”);
if(addTitle.value!==“”| | addTxt.value!==“”){
返回警报(“每台机器的形式。”)
}
if(notes==null){
注sobj=[];
}否则{
notesObj=JSON.parse(notes);
}
notesObj.findIndex((元素,索引)=>{
addTitle.value=element.title;
addTxt.value=element.text;
})
注:接头(索引1);
setItem(“notes”,JSON.stringify(notesObj));
showNotes();
}
showNotes();

在不声明变量的情况下分配变量会自动创建一个全局变量。从localStorage初始化
NotesAbj
的代码可能在页面加载时执行一次,而不是在每个函数中执行。“用于所有函数,不能仅启用?”-这(实际上是
localStorage.getItem(“注释”)`在此之前)保证
notesObj
将始终在
localStorage.notes
中存储最新的值,并且它将始终是一个数组。@Barmar谢谢,为什么我们还有json解析,我们不能直接在字符串中进行?因为
notesObj
是一个对象数组,本地存储只存储字符串。