js对象的方法

发布于:2022-12-24 ⋅ 阅读:(293) ⋅ 点赞:(0)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script>

        window.onload = function () {

            //     const myObj = {

            //         name: "John",

            //         age: 30,

            //         cars: [

            //             { name: "Ford", models: ["Fiesta", "Focus", "Mustang"] },

            //             { name: "BMW", models: ["320", "X3", "X5"] },

            //             { name: "Fiat", models: ["500", "Panda"] }

            //         ]

            //     };

            //     let x = "";

            //     for (let i in myObj.cars) {

            //         x += '<h1>' + myObj.cars[i].name + '</h1>';

            //         for (let j in myObj.cars[i].models) {

            //             x += myObj.cars[j].models[j]

            //         }

            //     };

            //     document.querySelector('#demo').innerHTML = x;

            //     const person = {

            //         name: "John",

            //         age: 30,

            //         city: "New York"

            //     };

            //     for (let o of Object.keys(person)) {

            //         console.log(o);

            //     };

            //     for (let x of Object.values(person)) {

            //         console.log(x);

            //     }

            //     const perso = {

            //         name: "John",

            //         today: new Date()

            //     };

            //    let test_strind = JSON.stringify(perso);

            //    console.log(test_strind);

            // const person = {

            //     name: "John",

            //     age: 30,

            //     city: "New York"

            // };

            // document.getElementById("demo").innerHTML = person.name+person.age+person.city;

            //将数组转化为字符串

            // const arr = ["John", "Peter", "Sally", "Jane"];

            // console.log(JSON.stringify(arr).replace('[]','r'));

            // function Person(first, last, age, eyecolor) {

            //     this.firstName = first;

            //     this.lastName = last;

            //     this.age = age;

            //     this.eyeColor = eyecolor;

            // }

            // // 向该函数中添加  展示姓名的方法

            // Person.prototype.showName = function(){

            //     console.log(this.age)

            // };

            // let test_Person = new Person("xiaolong","li",18,"index");

            // console.log(test_Person);

            // test_Person.showName();

            // Home Made Iterable

            // function myNumbers() {

            //     let n = 0;

            //     return {

            //         next: function () {

            //             n += 10;

            //             return { value: n, done: false };

            //         }

            //     };

            // }

            // // Create Iterable

            // const n = myNumbers();

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // console.log(n.next());

            // const web = {

            //     name: "后盾人",

            //     url: "houdunren.com",

            //     get site() {

            //         return `${this.name} ${this.url}`;

            //     },

            //     set site(value) {

            //         [this.name, this.url] = value.split(",");

            //     }

            // };

            // web.site = "后盾人,hdcms.com";

            // console.log(web.site);

            // let Request = {

            //     get token() {

            //         let con = localStorage.getItem('token');

            //         if (!con) {

            //             alert('请登录后获取token')

            //         } else {

            //             return con;

            //         }

            //     },

            //     set token(con) {

            //         localStorage.setItem('token', con);

            //     }

            // };

            // // Request.token = 'houdunren'

            // console.log(Request.token);

            // if (!window.JSON) {

            //     window.JSON = {

            //         parse: function (sJSON) { return eval('(' + sJSON + ')'); },

            //         stringify: (function () {

            //             var toString = Object.prototype.toString;

            //             var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };

            //             var escMap = { '"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t' };

            //             var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };

            //             var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;

            //             return function stringify(value) {

            //                 if (value == null) {

            //                     return 'null';

            //                 } else if (typeof value === 'number') {

            //                     return isFinite(value) ? value.toString() : 'null';

            //                 } else if (typeof value === 'boolean') {

            //                     return value.toString();

            //                 } else if (typeof value === 'object') {

            //                     if (typeof value.toJSON === 'function') {

            //                         return stringify(value.toJSON());

            //                     } else if (isArray(value)) {

            //                         var res = '[';

            //                         for (var i = 0; i < value.length; i++)

            //                             res += (i ? ', ' : '') + stringify(value[i]);

            //                         return res + ']';

            //                     } else if (toString.call(value) === '[object Object]') {

            //                         var tmp = [];

            //                         for (var k in value) {

            //                             if (value.hasOwnProperty(k))

            //                                 tmp.push(stringify(k) + ': ' + stringify(value[k]));

            //                         }

            //                         return '{' + tmp.join(', ') + '}';

            //                     }

            //                 }

            //                 return '"' + value.toString().replace(escRE, escFunc) + '"';

            //             };

            //         })()

            //     };

            // }

            // let hd = {

            //     name: "李小龙",

            //     age: 18,

            //     toJSON: function () {

            //         return {

            //             name: this.name,

            //             age: this.age

            //         }

            //     }

            // };

            // let index = JSON.stringify(hd, null, 2);

            // console.log(index);

            // //  这个方法就是将  普通对象转换为json  对象

            // //  json序列化就是  可以指定json  返回的格式

            // let lxl = {

            //     name: "xiaolong",

            //     age: 18,

            //     toJSON: function () {

            //         return {

            //             name: this.age

            //         }

            //     }

            // };

            // console.log(lxl);

            // let lxl_index = JSON.stringify(lxl, null, 2);

            // console.log(lxl_index);

            // let data = JSON.parse(lxl_index);

            // console.log(data);

            // const text = '["Ford", "BMW", "Audi", "Fiat"]';

            // const myArr = JSON.parse(text);

            // console.log(myArr);

            // const test_text = JSON.stringify(myArr);

            // console.log(test_text);

            // console.log(text)

            // const textd = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';

            // let lxl_textd = JSON.parse(textd);

            // console.log(lxl_textd);

            // //將字符串  轉換為函數

            // const text = '{"name":"John", "age":"function () {return 30;}", "city":"New York"}';

            // //使用json  提取字符串中的函数

            // const obj = JSON.parse(text);

            // console.log(obj);

            // // 提取函数

            // obj.age = eval("("+obj.age+")");

            // //  这样的话就成功的提取了  字符转中的函数

            // console.log(obj.name+''+'今年'+obj.age()+'la'+'依然很年轻');

            // Storing data:

            // const myObj = { name: "John", age: 31, city: "New York" };

            // const myJSON = JSON.stringify(myObj);

            // localStorage.setItem("testJSON", myJSON);

            // // Retrieving data:

            // let text = localStorage.getItem("testJSON");

            // let obj = JSON.parse(text);

            // document.getElementById("demo").innerHTML = obj.name;

            //     const myJSON = '{"name":"John", "age":30, "cars":["Ford", "BMW", "Fiat"]}';

            //     const myObj = JSON.parse(myJSON);

            //     console.log(myJSON);

            //     //遍历数组对象当中的数组

            //    for(let i =0;i<myObj.length;i++){

            //        console.log(myObj[i])

            //    }

            const myObj = { name: "John", age: 31, city: "New York" };

            const myJSON = JSON.stringify(myObj);

            window.location = "demo_json.php?x=" + myJSON;

        }

    </script>

</head>

<body>

    <p id="demo"></p>

</body>

</html>


网站公告

今日签到

点亮在社区的每一天
去签到