JavaScript, how to implement data binding

Source: Internet
Author: User

In the three frameworks to preempt the front-end development of technology in the era, the basic framework is to achieve data binding,

Today we're going to decrypt the principle of data binding,

The principle is about a key thing, Object.prototype.__definesetter and Object.prototype.__definegetter.

MDN Address: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineSetter__

Now I'm going to implement a function that binds a value on an object with the innerHTML of the DOM element.

function bind (Ele,obj,key) {    var index={        init:function () {            //check for an initial value, or copy            if (key in obj) {                Ele.innerhtml=obj[key]            }            this.bind ()        },        bind:function () {            obj.__definesetter__ (key,function (v) {                ele.innerhtml=v            })            obj.__definegetter__ (Key,function () {                return ele.innerhtml            })        },        _do:function () {}    }    index.init ()}

However, this function has a disadvantage, can only bind a value to an element of the innerHTML, an element can be bound to multiple values, but there is no way to bind a value of multiple DOM element properties, but can be modified to achieve, the implementation of the code will be very long, here can only be a point.

JavaScript, how to implement data binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.